最大流-HDU-3605-Escape
2016-05-18 22:38
417 查看
Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 8376 Accepted Submission(s): 1893
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
题意:
一共有n个人,m个星球,每个人都有自己适应居住的星球,每个星球有自己容纳上限,现在问是否可以让所有人都住到这m个星球上去。
题解:
因为n非常大(1E5),而m又非常小(10),所以可以发现,人只会有2^m种情况,那么进行压缩后,先统计每种情况的人数,再从每种情况建网跑最大流就可以了。
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 8376 Accepted Submission(s): 1893
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
题意:
一共有n个人,m个星球,每个人都有自己适应居住的星球,每个星球有自己容纳上限,现在问是否可以让所有人都住到这m个星球上去。
题解:
因为n非常大(1E5),而m又非常小(10),所以可以发现,人只会有2^m种情况,那么进行压缩后,先统计每种情况的人数,再从每种情况建网跑最大流就可以了。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <queue> #include <vector> #include <set> #include <map> #include <utility> using namespace std; const int MAXN = 2048;//点数的最大值 const int MAXM = 400010;//边数的最大值 const int INF = 0x3f3f3f3f; struct Edge { int to,next,cap,flow; } edge[MAXM]; //注意是MAXM int tol; int head[MAXN]; int gap[MAXN],dep[MAXN],cur[MAXN]; void init() { tol = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w,int rw = 0) { edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } int Q[MAXN]; void BFS(int start,int end) { memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); gap[0] = 1; int front = 0, rear = 0; dep[end] = 0; Q[rear++] = end; while(front != rear) { int u = Q[front++]; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(dep[v] != -1)continue; Q[rear++] = v; dep[v] = dep[u] + 1; gap[dep[v]]++; } } } int S[MAXN]; int sap(int start,int end,int N) { BFS(start,end); memcpy(cur,head,sizeof(head)); int top = 0; int u = start; int ans = 0; while(dep[start] < N) { if(u == end) { int Min = INF; int inser; for(int i = 0; i < top; i++) if(Min > edge[S[i]].cap - edge[S[i]].flow) { Min = edge[S[i]].cap - edge[S[i]].flow; inser = i; } for(int i = 0; i < top; i++) { edge[S[i]].flow += Min; edge[S[i]^1].flow -= Min; } ans += Min; top = inser; u = edge[S[top]^1].to; continue; } bool flag = false; int v; for(int i = cur[u]; i != -1; i = edge[i].next) { v = edge[i].to; if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u]) { flag = true; cur[u] = i; break; } } if(flag) { S[top++] = cur[u]; u = v; continue; } int Min = N; for(int i = head[u]; i != -1; i = edge[i].next) if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) { Min = dep[edge[i].to]; cur[u] = i; } gap[dep[u]]--; if(!gap[dep[u]])return ans; dep[u] = Min + 1; gap[dep[u]]++; if(u != start)u = edge[S[--top]^1].to; } return ans; } long long int n,num[1<<11]; int m,total; int main() { int tmp,tmp1; while(scanf("%lld%d",&n,&m)!=EOF) { init(); total=1<<m; memset(num,0,sizeof(num)); for(long long int i=1;i<=n;i++) { tmp1=0; for(int j=0;j<m;j++) { scanf("%d",&tmp); if(tmp) tmp1+=(1<<j); } num[tmp1]++; } for(long long int i=0;i<total;i++) { addedge(total+m,i,num[i]); for(int j=0;j<m;j++) { if(i&(1<<j)) addedge(i,total+j,num[i]); } } for(int i=0;i<m;i++) { scanf("%d",&tmp); addedge(total+i,total+m+1,tmp); } if(sap(total+m,total+m+1,total+m+2)==n) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- Oracle 10G安装说明---谷营中西软件科技园
- 一点一滴学shell
- python反射
- 离散数学知识点整理
- 认识-冒泡算法
- Java HashMap的工作原理
- okhttp封装时,提示 cannot resolve method OkHttpClient setConnectTimeout() 函数
- Demo6 TabLayout(上)你会用适配器吗:ViewPager
- android直接杀死一个应用与通过回退键退出一个应用的区别
- 这才叫做面试
- 网络黑客攻防学习平台之基础关第二题
- 设计模式总结篇系列:组合模式(Composite)
- ubuntu 16.04 技巧
- Oracle Golden Gate基本配置
- SQL触发器调用.NET的类方法续SQLCLR应用
- Kivy python在ubuntu下安装使用
- 数据库学习3 Distinct Group By
- Kivy python在ubuntu下安装使用
- 内部类
- 退耦和滤波[转]