标签:logs ace ssi char mis lines ant 容量 his
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 10920 Accepted Submission(s): 2630
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 }
标签:logs ace ssi char mis lines ant 容量 his
原文地址:http://www.cnblogs.com/Penn000/p/7424929.html