HDU 3605Escape(最大流+状压DP)@
2017-01-18 00:24
295 查看
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.
InputMore 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
OutputDetermine whether all people can live up to these stars
If you can output YES, otherwise output NO.
Sample Input
Sample Output
题意:
现有n个人要移居到m个星球去,给定一个n*m的矩阵,第 i 行第 j 列如果为1,表示第 i 个人可以去第 j 个星球,如果为0,表示不可以去。然后给出这m个星球都最多分别能住多少人,问你n个人是不是都能找到星球住? (1 <= n <= 100000), (1 <= m<= 10)
分析:
明显的最大流问题,不过n的数目最多100W,如果直接这么算(即连数百w的容量为1的边)肯定超时. 这里m最多只有10个,所以一个人对于星球的选择最多有2^10=1024种,所以我们图左边的节点可以用这1024种不同的选择来表示.
左边节点表示选择方案(编号从0到1023),右边的节点表示m个星球(编号从1023+1到1023+m),源点s为1024+m,汇点t为1025+m.
比如左边的节点为7时(二进制形式为00 0000 0111),即表示选择第0,1,2这三个星球,所以我们连接7号节点与右边的0号,1号,2号星球节点,容量为 只能选0,1,2三个星球的人的总数目.
源点s到每个选择方案i有边(s,i,用该方案的人数)
方案i中如果有选择星球j,那么有边(i,j,INF)
星球j到汇点t有边(j,t,can[j])
can[j]表示星球j能容纳的最大人数.
最终我们看 max_flow 是否== n 即可.
what all of people can live in these planets.
InputMore 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
OutputDetermine 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个星球去,给定一个n*m的矩阵,第 i 行第 j 列如果为1,表示第 i 个人可以去第 j 个星球,如果为0,表示不可以去。然后给出这m个星球都最多分别能住多少人,问你n个人是不是都能找到星球住? (1 <= n <= 100000), (1 <= m<= 10)
分析:
明显的最大流问题,不过n的数目最多100W,如果直接这么算(即连数百w的容量为1的边)肯定超时. 这里m最多只有10个,所以一个人对于星球的选择最多有2^10=1024种,所以我们图左边的节点可以用这1024种不同的选择来表示.
左边节点表示选择方案(编号从0到1023),右边的节点表示m个星球(编号从1023+1到1023+m),源点s为1024+m,汇点t为1025+m.
比如左边的节点为7时(二进制形式为00 0000 0111),即表示选择第0,1,2这三个星球,所以我们连接7号节点与右边的0号,1号,2号星球节点,容量为 只能选0,1,2三个星球的人的总数目.
源点s到每个选择方案i有边(s,i,用该方案的人数)
方案i中如果有选择星球j,那么有边(i,j,INF)
星球j到汇点t有边(j,t,can[j])
can[j]表示星球j能容纳的最大人数.
最终我们看 max_flow 是否== n 即可.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <queue> using namespace std; const int N = 1e5+100; const int inf = 0x3f3f3f3f; struct node { int to, cap, rev; }; vector<node>p ; void add(int u,int v,int w) { node e; e.to=v, e.cap=w, e.rev=p[v].size(); p[u].push_back(e); e.to=u, e.cap=0, e.rev=p[u].size()-1; p[v].push_back(e); return ; } int d ; int bfs(int s,int t) { memset(d,-1,sizeof(d)); d[s]=0; queue<int>q; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0; i<p[u].size(); i++) { node e=p[u][i]; if(d[e.to]<0&&e.cap>0) { d[e.to]=d[u]+1; q.push(e.to); } } } return d[t]==-1; } int lter ; int dfs(int s,int t,int flow) { if(s==t) return flow; for(int &i=lter[s]; i<p[s].size(); i++) { node &e=p[s][i]; if(d[e.to]==d[s]+1&&e.cap>0) { int f=dfs(e.to,t,min(flow,e.cap)); if(f>0) { e.cap-=f, p[e.to][e.rev].cap+=f; return f; } } } return 0; } int dinic(int s,int t) { int flow=0, f; while(1) { while(bfs(s, t)) return flow; memset(lter,0,sizeof(lter)); while(f=dfs(s, t, inf),f>0) { flow+=f; } } return flow; } int dp ; int main() { int n, m, x; while(scanf("%d %d", &n, &m)!=EOF) { memset(dp,0,sizeof(dp)); for(int i=1; i<=n; i++) { int state=0; for(int j=0; j<m; j++) { scanf("%d", &x); if(x==1) state|=(1<<j); } dp[state]++; } int s=2000, e=2001; for(int i=0;i<m;i++) { scanf("%d", &x); add((1<<m)+i,e,x); } for(int i=0;i<(1<<m);i++) { if(dp[i]) { add(s,i,dp[i]); for(int j=0;j<m;j++) { if(i&(1<<j)) add(i,(1<<m)+j,inf); } } } int sum=dinic(s,e); if(sum>=n)printf("YES\n"); else printf("NO\n"); for(int i=0; i<=e; i++) p[i].clear(); } return 0; }
相关文章推荐
- 杨辉三角形
- Hadoop2.5.2学习01--mapreduce统计单词数
- 【LeetCode】 313. Super Ugly Number
- Django之Helloworld
- fedora 18~20 中Qt 5.2.1 解决连接mysql数据库出现QMYSQL driver not loaded的问题
- 【leetcode】【Easy】【463. Island Perimeter】
- Java实现常用简单负载均衡(LoadBalance)
- Lightoj 1166 - Old Sorting (简单贪心)
- javaGUI基本知识讲解
- javaGUI基本知识讲解
- 疯狼算法日记0002-句子中每个单词逆序
- LaTeX快速上手
- Firebug: 已拦截跨源请求:同源策略禁止读取位于XXX的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-
- 《深入理解JVM》--内存管理机制
- 【设计模式】—-(6)适配器模式(结构型)
- react生命周期方法
- javaGUI之基本布局方式
- javaGUI之基本布局方式
- 读Linux源码分析ov5640在三星exynos4412平台上的使用
- Swift 3.0 版本变化