Escape
Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 360564-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
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
Source
解题:最大流。。。关键是 ,人数很多,星球很少,最多10个星球,最多1<<10种状态,故很多重态,统计重态,避免超时。代码在vc的编译器可以跑过,g++的不知道为毛超时了。
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 ",maxcap == n?"YES":"NO"); 106 } 107 return 0; 108 }
多重匹配匈牙利解法
1 #include <bits/stdc++.h> 2 using namespace std; 3 int n,m,limit[15]; 4 bool g[100010][15],T[15]; 5 vector<int>Link[15]; 6 bool match(int u) { 7 for(int i = 0; i < m; ++i) { 8 if(g[u][i] && !T[i]) { 9 T[i] = true; 10 if(Link[i].size() < limit[i]) { 11 Link[i].push_back(u); 12 return true; 13 } 14 for(int j = Link[i].size()-1; j >= 0; --j) { 15 if(match(Link[i][j])) { 16 Link[i][j] = u; 17 return true; 18 } 19 } 20 21 } 22 } 23 return false; 24 } 25 bool solve() { 26 for(int i = 0; i < n; ++i) { 27 memset(T,false,sizeof T); 28 if(!match(i)) return false; 29 } 30 return true; 31 } 32 inline bool scan_d(int &num) { 33 char in; 34 bool IsN=false; 35 in=getchar(); 36 if(in==EOF) return false; 37 while(in!='-'&&(in<'0'||in>'9')) in=getchar(); 38 if(in=='-') { 39 IsN=true; 40 num=0; 41 } else num=in-'0'; 42 while(in=getchar(),in>='0'&&in<='9') { 43 num*=10,num+=in-'0'; 44 } 45 if(IsN) num=-num; 46 return true; 47 } 48 int main() { 49 int tmp; 50 while(~scanf("%d%d",&n,&m)) { 51 memset(g,false,sizeof g); 52 memset(limit,0,sizeof limit); 53 for(int i = 0; i < 11; ++i) 54 Link[i].clear(); 55 for(int i = 0; i < n; ++i) 56 for(int j = 0; j < m; ++j) { 57 scan_d(tmp); 58 g[i][j] = tmp > 0; 59 } 60 for(int i = 0; i < m; ++i) 61 scanf("%d",limit + i); 62 puts(solve()?"YES":"NO"); 63 } 64 return 0; 65 }