HDU 3605 Escape(多重匹配||状压缩点的网络流)

来源:互联网 发布:ubuntu系统有什么用 编辑:程序博客网 时间:2024/05/21 16:55

Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 11151    Accepted Submission(s): 2676


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 1112 21 01 01 1
 

Sample Output
YESNO
 

Source
2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU 
 

题意:
n个人,m个星球,给你n个人的意向选择的星球和星球的容量,让你判断能不能全部分配好。

POINT:
法1:
多重匹配,多重的匈牙利算法。

法2:
因为星球最多10个,也就是最多1<<10种可能选择的方案,把人都归类为这些方案里。
那么就是缩点了。不然点很多超时。



法1:
#include <vector>#include <stdio.h>#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;#define LL long longtypedef pair<int,int> pr;const int maxn =100100;const int inf = 0x3f3f3f3f;int n,m;int mp[maxn+3][13];int num[13];struct node{    int p[maxn];    int num;}qiu[13];int vis[13];bool dfs(int u){    for(int i=1;i<=m;i++)    {        if(vis[i]||!mp[u][i]) continue;        vis[i]=1;        if(qiu[i].num<num[i])        {            qiu[i].num++;            qiu[i].p[qiu[i].num]=u;            return true;        }        else        {            for(int j=1;j<=qiu[i].num;j++)            {                if(dfs(qiu[i].p[j]))                {                    qiu[i].p[j]=u;                    return true;                }            }        }    }    return false;}bool haha(){    for(int i=1;i<=n;i++)    {        memset(vis,0,sizeof vis);        if(dfs(i))        {            continue;        }        else            return false;    }    return true;}int main(){    while(~scanf("%d %d",&n,&m))    {        for(int i=1;i<=m;i++) qiu[i].num=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf("%d",&mp[i][j]);            }        }        for(int i=1;i<=m;i++) scanf("%d",&num[i]);        if(haha())        {            printf("YES\n");        }        else            printf("NO\n");    }    return 0;}


法2:
#include <vector>#include <stdio.h>#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;#define LL long longtypedef pair<int,int> pr;const int maxn =2222;const int inf = 0x3f3f3f3f;int n,m,s,t;int num[maxn];int sz=0;struct edge{    int from,to,flow,cap,nxt;}len[4000110];int head[maxn];void add(int u,int v,int c){    len[sz].from=u;    len[sz].to=v;    len[sz].flow=0;    len[sz].cap=c;    len[sz].nxt=head[u];    head[u]=sz++;            len[sz].from=v;    len[sz].to=u;    len[sz].flow=0;    len[sz].cap=0;    len[sz].nxt=head[v];    head[v]=sz++;}void init(){    sz=0;    memset(num,0,sizeof num);    memset(head,-1,sizeof head);}int d[maxn];bool bfs(){    int q[maxn<<1];    int vis[maxn];    memset(vis,0,sizeof vis);    memset(d,0,sizeof d);    d[s]=1;    int hd=1,wei=1;    vis[s]=1;    q[wei++]=s;    while(hd!=wei)    {        int u=q[hd++];        for(int i=head[u];i!=-1;i=len[i].nxt)        {            if(!vis[len[i].to]&&len[i].cap>len[i].flow)            {                vis[len[i].to]=1;                d[len[i].to]=d[u]+1;                q[wei++]=len[i].to;              //  if(d[t]>0) return 1;            }        }    }    return vis[t];}int cur[maxn];int dfs(int u,int a){    if(u==t||a==0) return a;    int flw=0,f;    for(int i=head[u];i!=-1;i=len[i].nxt)    {        if(d[len[i].to]-1==d[u]&&(f=dfs(len[i].to,min(a,len[i].cap-len[i].flow)))>0)        {            flw+=f;            len[i].flow+=f;            len[i^1].flow-=f;            a-=f;        }        if(a==0) break;    }    if(flw==0) d[u]=-1;    return flw;}int maxflow(){    int ans=0;    while(bfs())    {        ans+=dfs(s,inf);    }    return ans;    }int main(){    while(~scanf("%d %d",&n,&m))    {        init();        for(int i=1;i<=n;i++)        {            int x=0;            for(int j=0;j<m;j++)            {                int a;scanf("%d",&a);                if(a==1)                    x+=(1<<j);            }            num[x]++;        }        s=0,t=1024+m+1;        // s=0,1-1024 人的种类,1024+1-2014+m 星球 , t=2014+m+1        for(int i=1;i<=1024;i++)        {            if(num[i]>0)            {                for(int j=0;j<m;j++)                {                    if((i&(1<<j))>=1)                    {                        add(i+1,1024+j+1,inf);                    }                }                add(0,i+1,num[i]);            }        }        for(int i=0;i<m;i++){            int a;            scanf("%d",&a);            add(1024+i+1,t,a);        }        if(maxflow()>=n)        {            printf("YES\n");        }        else            printf("NO\n");            }    return 0;}



原创粉丝点击