Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 11707 Accepted Submission(s): 2847
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
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.
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题意:有n个人要移居m个星球,给出每个合适的星球,每个星球最多能容纳的人数,问是否所有人都可以移居。思路:如果直接建那么多边求最大流,时间会超,这么多人肯定有许多人的选择是重复的,而且总方案数不超过210,根据这个,我们可以对人进行缩点,选择一样的就把数量累加起来,同理,源点连过来的边修改相应的容量,连出去的边也修改。代码:#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <cmath> #include <queue> #include <vector> using namespace std; const int maxn=200100; const int maxm=1000100; const int inf=0x3f3f3f3f; int n,m; int s,t; int cnt; int Next[maxm]; int depth[maxn]; int head[maxn]; int cur[maxn]; int num[maxn]; struct edge{ int u,v,w; }e[maxm]; void addedge(int u,int v,int w) { cnt++; Next[cnt]=head[u]; head[u]=cnt; e[cnt].u=u; e[cnt].v=v; e[cnt].w=w; cnt++; Next[cnt]=head[v]; head[v]=cnt; e[cnt].u=v; e[cnt].v=u; e[cnt].w=0; } int bfs() { memset(depth,0,sizeof(depth)); depth[s]=1; queue<int>q; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=Next[i]) { int v=e[i].v; if(depth[v]==0&&e[i].w>0) { depth[v]=depth[u]+1; q.push(v); } } } if(depth[t]==0)return 0; return 1; } int dfs(int u,int w) { if(u==t) return w; int f=0; for(int i=head[u];i!=-1;i=Next[i]) { int v=e[i].v; if(depth[v]==depth[u]+1&&e[i].w>0) { int d=dfs(v,min(w-f,e[i].w)); e[i].w-=d; e[i^1].w+=d; f+=d; if(f==w)break; } } if(!f)depth[u]=0; return f; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int i,j; cnt=-1; memset(head,-1,sizeof(head)); int a; s=0; t=n+m+1; int k; memset(num,0,sizeof(num)); for(i=1;i<=n;i++) { k=0; for(j=0;j<m;j++) { scanf("%d",&a); if(a==1) k|=(1<<j); } num[k]++; } for(i=0;i<(1<<m);i++) { if(num[i]==0)continue; addedge(s,i+1,num[i]); for(j=0;j<m;j++) { if(i&(1<<j)) addedge(i+1,n+j+1,num[i]); } } for(i=1;i<=m;i++) { scanf("%d",&a); addedge(i+n,t,a); } int ans=0; while(bfs()) { for(i=0;i<t+2;i++) { cur[i]=head[i]; } while(int di=dfs(s,inf)) { ans+=di; } } if(ans==n) printf("YES\n"); else printf("NO\n"); } return 0; }