hdu 3605(二分图的多重匹配 | 网络流)

时间:2021-04-06 06:11:33

 一开始以为这题是二分图的匹配,然后用匈牙利做,TLE,然后用HK 做, TLE。。。

最后发现是用网络流做的,而且要将100000个点压缩, 因为每个点的可能选择情况有2^10 ,所以根据这个可以将图压缩成1000*10大小的图,这样用网络流就没有压力了...

网络流建模很重要.! 

 

Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2483    Accepted Submission(s): 687


Problem Description
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
 

 

Input
More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
 

 

Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
 

 

Sample Input
1 1 1 1 2 2 1 0 1 0 1 1
 

 

Sample Output
YES NO
 

 

Source
 

 

Recommend
lcy
 

 

Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2483    Accepted Submission(s): 687


Problem Description
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
 

 

Input
More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
 

 

Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
 

 

Sample Input
1 1 1 1 2 2 1 0 1 0 1 1
 

 

Sample Output
YES NO
 

 

Source
 

 

Recommend
lcy
 

 

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 100100
#define K 1111
#define INF 0x3fffffff

struct  node
{
    int to,next,w;
}edge[N];

int cnt,pre[2200];
int n,m;
int s,t;
int g[N][11];
int save[2200];
int tt;
int gap[2200],lv[2200];

void add_edge(int u,int v,int w)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].next=pre[u];
    pre[u]=cnt++;
}

int sdfs(int k,int w)
{
    if(k==t) return w;
    int f=0;
    int mi=tt;
    for(int p=pre[k];p!=-1;p=edge[p].next)
    {
        int v=edge[p].to;
        if(edge[p].w!=0)
        {
            if( lv[k]==lv[v]+1 )
            {
                int tmp=sdfs(v,min(edge[p].w,w-f));
                f += tmp;
                edge[p].w -= tmp;
                edge[p^1].w += tmp;
                if( f==w || lv[s]==tt ) return f;
            }
            if(lv[v] < mi) mi=lv[v];
        }
    }
    if(f==0)
    {
        gap[lv[k]]--;
        if( gap[lv[k]]==0 )
        {
            lv[s]=tt;
            return 0;
        }
        lv[k]=mi+1;
        gap[lv[k]]++;
    }
    return f;
}

int sap()
{
    int sum=0;
    tt=t+1;
    memset(gap,0,sizeof(gap));
    memset(lv,0,sizeof(lv));
    gap[0]=tt;
    while(lv[s]<tt)
    {
        sum+=sdfs(s,INF);
    }
    return sum;
}

int main()
{
    while( scanf("%d%d",&n,&m)!=EOF )
    {
        s=0; t=K+m+1;
        cnt=0;
        memset(pre,-1,sizeof(pre));
        memset(save,0,sizeof(save));
        for(int i=1;i<=n;i++)
        {
            int tmp=0;
            for(int j=0;j<m;j++)
            {
                int bit;
                scanf("%d",&bit);
                tmp = tmp*2+bit;
            }
            save[tmp]++;
        }
        for(int i=0;i<1100;i++)
        {
            if(save[i]!=0)
            {
                add_edge(s,i+1,save[i]);
                add_edge(i+1,s,0);
                int tmp=m;
                int num=i;
                while(num)
                {
                    int k = num&1;
                    if( k==1 )
                    {
                        add_edge(i+1,K+tmp,INF);
                        add_edge(K+tmp,i+1,0);
                    }
                    tmp--;
                    num >>= 1;
                }
            }
        }
        for(int i=1;i<=m;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            add_edge(K+i,t,tmp);
            add_edge(t,K+i,0);
        }
        if(sap()==n) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}