博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU3605(KB11-M 状态压缩+最大流)
阅读量:5342 次
发布时间:2019-06-15

本文共 3956 字,大约阅读时间需要 13 分钟。

Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 10920    Accepted Submission(s): 2630

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

 
由于n很大,直接建图会T,但是m很小,因此根据一个人在能否在m个星球上生存的状态,可以压缩为一个m位二进制数。最多有2^10种状态。
源点向每一种状态连边,容量为该状态的人数。
每个状态向该状态下能够生存的星球连边,容量为该状态的人数。
每个星球向汇点连边,容量为星球最多能承受的人数。
1 //2017-08-24  2 #include 
3 #include
4 #include
5 #include
6 #include
7 8 using namespace std; 9 10 const int N = 110000; 11 const int M = 5100100; 12 const int INF = 0x3f3f3f3f; 13 int head[N], tot; 14 struct Edge{ 15 int next, to, w; 16 }edge[M]; 17 18 void add_edge(int u, int v, int w){ 19 edge[tot].w = w; 20 edge[tot].to = v; 21 edge[tot].next = head[u]; 22 head[u] = tot++; 23 24 edge[tot].w = 0; 25 edge[tot].to = u; 26 edge[tot].next = head[v]; 27 head[v] = tot++; 28 } 29 30 struct Dinic{ 31 int level[N], S, T; 32 void init(int _S, int _T){ 33 S = _S; 34 T = _T; 35 tot = 0; 36 memset(head, -1, sizeof(head)); 37 } 38 bool bfs(){ 39 queue
que; 40 memset(level, -1, sizeof(level)); 41 level[S] = 0; 42 que.push(S); 43 while(!que.empty()){ 44 int u = que.front(); 45 que.pop(); 46 for(int i = head[u]; i != -1; i = edge[i].next){ 47 int v = edge[i].to; 48 int w = edge[i].w; 49 if(level[v] == -1 && w > 0){ 50 level[v] = level[u]+1; 51 que.push(v); 52 } 53 } 54 } 55 return level[T] != -1; 56 } 57 int dfs(int u, int flow){ 58 if(u == T)return flow; 59 int ans = 0, fw; 60 for(int i = head[u]; i != -1; i = edge[i].next){ 61 int v = edge[i].to, w = edge[i].w; 62 if(!w || level[v] != level[u]+1) 63 continue; 64 fw = dfs(v, min(flow-ans, w)); 65 ans += fw; 66 edge[i].w -= fw; 67 edge[i^1].w += fw; 68 if(ans == flow)return ans; 69 } 70 if(ans == 0)level[u] = -1; 71 return ans; 72 } 73 int maxflow(){ 74 int flow = 0, f; 75 while(bfs()) 76 while((f = dfs(S, INF)) > 0) 77 flow += f; 78 return flow; 79 } 80 }dinic; 81 82 char str[N]; 83 int status[1<<12];//status[S]表示状态为S的人数,例如S=1010,表示可以在2号和4号星球上生存(从低位标号) 84 85 int main() 86 { 87 std::ios::sync_with_stdio(false); 88 //freopen("inputM.txt", "r", stdin); 89 int n, m; 90 while(cin>>n>>m){ 91 int s = 0, t = n+m+1; 92 dinic.init(s, t); 93 int w; 94 memset(status, 0, sizeof(status)); 95 for(int i = 1; i <= n; i++){ 96 int tmp = 0; 97 for(int j = 1; j <= m; j++){ 98 tmp <<= 1; 99 cin>>w;100 tmp |= w;101 }102 status[tmp]++;103 }104 for(int i = 1; i <= (1<<10); i++){105 if(status[i]){106 add_edge(s, i, status[i]);107 for(int j = 1; j <= m; j++){108 if(i & (1<<(j-1)))109 add_edge(i, n+j, status[i]);110 }111 }112 }113 int sum = 0;114 for(int i = 1; i <= m; i++){115 cin>>w;116 sum += w;117 add_edge(n+i, t, w);118 }119 if(sum < n){120 cout<<"NO"<

 

转载于:https://www.cnblogs.com/Penn000/p/7424929.html

你可能感兴趣的文章
元素定位(2)
查看>>
学习计划
查看>>
springMVC+MyBatis+Spring 整合(2)
查看>>
Mac下的svn Cornerstone使用和下载地址 (3.03版本)
查看>>
1054. The Dominant Color (20)
查看>>
spring.net +dapper 打造简易的DataAccess 工具类.
查看>>
Android快照与截屏
查看>>
Safari 3D transform变换z-index层级渲染异常的研究
查看>>
cmd命令行--切换盘符
查看>>
老王讲架构:负载均衡
查看>>
poj 3295 Tautology
查看>>
hdu 吃糖果
查看>>
[LeetCode] Reverse Linked List II
查看>>
CSS IE6/7/8, Firefox, Safari, Chrome, Opera Hack使用简要归纳(转)
查看>>
ACE admin 后台管理框架
查看>>
ASP.NET Core学习之二 菜鸟踩坑
查看>>
转:redis windows下的环境搭建
查看>>
改变图片效果
查看>>
Python的数据类型--数字--字符串
查看>>
session的方法
查看>>