HDU3605(KB11-M 状态压缩+最大流)
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
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
由于n很大,直接建图会T,但是m很小,因此根据一个人在能否在m个星球上生存的状态,可以压缩为一个m位二进制数。最多有2^10种状态。
源点向每一种状态连边,容量为该状态的人数。
每个状态向该状态下能够生存的星球连边,容量为该状态的人数。
每个星球向汇点连边,容量为星球最多能承受的人数。
1 //2017-08-24 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 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<int> 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"<<endl; 121 continue; 122 } 123 if(dinic.maxflow() == n)cout<<"YES"<<endl; 124 else cout<<"NO"<<endl; 125 } 126 return 0; 127 }