首页 > 其他 > 详细

HDU 3605 Escape

时间:2014-10-18 20:56:06      阅读:353      评论:0      收藏:0      [点我收藏+]

Escape

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3605
64-bit integer IO format: %I64d      Java class name: Main
 
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

 
解题:最大流。。。关键是 ,人数很多,星球很少,最多10个星球,最多1<<10种状态,故很多重态,统计重态,避免超时。代码在vc的编译器可以跑过,g++的不知道为毛超时了。
 
bubuko.com,布布扣
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 200010;
 18 struct arc{
 19     int to,flow,next;
 20     arc(int x = 0,int y = 0,int z = -1){
 21         to = x;
 22         flow = y;
 23         next = z;
 24     }
 25 };
 26 arc e[1000000];
 27 int head[maxn],d[maxn],cur[maxn];
 28 int tot,s,t,n,m;
 29 void add(int u,int v,int flow){
 30     e[tot] = arc(v,flow,head[u]);
 31     head[u] = tot++;
 32     e[tot] = arc(u,0,head[v]);
 33     head[v] = tot++;
 34 }
 35 int q[1000000],hd,tl;
 36 bool bfs(){
 37     //queue<int>q;
 38     hd = tl = 0;
 39     memset(d,-1,sizeof(d));
 40     d[s] = 1;
 41     //q.push(s);
 42     q[tl++] = s;
 43     while(hd < tl){
 44         int u = q[hd++];
 45         //q.pop();
 46         for(int i = head[u]; ~i; i = e[i].next){
 47             if(e[i].flow && d[e[i].to] == -1){
 48                 d[e[i].to] = d[u] + 1;
 49                 //q.push(e[i].to);
 50                 q[tl++] = e[i].to;
 51             }
 52         }
 53     }
 54     return d[t] > -1;
 55 }
 56 int dfs(int u,int low){
 57     if(u == t) return low;
 58     int tmp = 0,a;
 59     for(int &i = cur[u]; ~i; i = e[i].next){
 60         if(e[i].flow && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(low,e[i].flow)))){
 61             e[i].flow -= a;
 62             e[i^1].flow += a;
 63             tmp += a;
 64             low -= a;
 65             if(!low) break;
 66         }
 67     }
 68     if(!tmp) d[u] = -1;
 69     return tmp;
 70 }
 71 int main() {
 72     int tmp,b[1<<12];
 73     while(~scanf("%d %d",&n,&m)){
 74         memset(head,-1,sizeof(head));
 75         memset(b,0,sizeof(b));
 76         for(int i = 0; i < n; ++i){
 77             int p = 0;
 78             for(int j = 0; j < m; ++j){
 79                 scanf("%d",&tmp);
 80                 p = p<<1|(tmp&1);
 81             }
 82             b[p]++;
 83         }
 84         s = (1<<m) + m;
 85         t = (1<<m) + m + 1;
 86         for(int i = tot = 0; i < (1<<m); ++i){
 87             if(b[i]){
 88                 add(s,i,b[i]);
 89                 for(int j = 0; j < m; ++j){
 90                     if(i&(1<<j)){
 91                         add(i,(1<<m)+j,b[i]);
 92                     }
 93                 }
 94             }
 95         }
 96         for(int i = 0; i < m; ++i){
 97             scanf("%d",&tmp);
 98             add((1<<m)+i,t,tmp);
 99         }
100         int maxcap = 0;
101         while(bfs()){
102             memcpy(cur,head,sizeof(head));
103             maxcap += dfs(s,INF);
104         }
105         printf("%s\n",maxcap == n?"YES":"NO");
106     }
107     return 0;
108 }
View Code

 

HDU 3605 Escape

原文:http://www.cnblogs.com/crackpotisback/p/4033610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!