标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16555 | Accepted: 7416 |
Description
Input
Output
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
Source
#include <stdio.h> #include <string.h> #define inf 0x3fffffff #define maxn 110 #define maxm 1002 int pig[maxm], m, n, sink; int G[maxn][maxn], queue[maxn]; bool vis[maxn]; int Layer[maxn]; bool countLayer() { memset(Layer, 0, sizeof(Layer)); int id = 0, front = 0, now, i; Layer[0] = 1; queue[id++] = 0; while(front < id) { now = queue[front++]; for(i = 0; i <= sink; ++i) if(G[now][i] && !Layer[i]) { Layer[i] = Layer[now] + 1; if(i == sink) return true; else queue[id++] = i; } } return false; } int Dinic() { int minCut, pos, maxFlow = 0; int i, id = 0, u, v, now; while(countLayer()) { memset(vis, 0, sizeof(vis)); vis[0] = 1; queue[id++] = 0; while(id) { now = queue[id - 1]; if(now == sink) { minCut = inf; for(i = 1; i < id; ++i) { u = queue[i - 1]; v = queue[i]; if(G[u][v] < minCut) { minCut = G[u][v]; pos = u; } } maxFlow += minCut; for(i = 1; i < id; ++i) { u = queue[i - 1]; v = queue[i]; G[u][v] -= minCut; G[v][u] += minCut; } while(queue[id - 1] != pos) vis[queue[--id]] = 0; } else { for(i = 0; i <= sink; ++i) { if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) { vis[i] = 1; queue[id++] = i; break; } } if(i > sink) --id; } } } return maxFlow; } int main() { //freopen("stdin.txt", "r", stdin); int i, keys, num; while(scanf("%d%d", &m, &n) == 2) { sink = n + 1; for(i = 1; i <= m; ++i) scanf("%d", &pig[i]); memset(G, 0, sizeof(G)); for(i = 1; i <= n; ++i) { scanf("%d", &keys); while(keys--) { scanf("%d", &num); if(pig[num] >= 0) { G[0][i] += pig[num]; // 0 is source pig[num] = -i; // 这里是标记第num个猪圈联通的第一个人 } else G[-pig[num]][i] = inf; } scanf("%d", &G[i][sink]); } printf("%d\n", Dinic()); } return 0; }
2015.4.20
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 105; const int inf = 0x3f3f3f3f; int G[maxn][maxn], M, N, S, T; int pigHouse[maxn*10]; int Dinic(int s, int t); void getMap() { memset(G, 0, sizeof(G)); S = 0; T = N + 1; int i, j, K, pos; for (i = 1; i <= M; ++i) scanf("%d", &pigHouse[i]); for (i = 1; i <= N; ++i) { scanf("%d", &K); while (K--) { scanf("%d", &pos); if (pigHouse[pos] >= 0) { G[S][i] += pigHouse[pos]; pigHouse[pos] = -i; } else { G[-pigHouse[pos]][i] = inf; } } scanf("%d", &G[i][T]); } } void solve() { cout << Dinic(S, T) << endl; } int main() { while (cin >> M >> N) { getMap(); solve(); } return 0; } int queue[maxn]; bool vis[maxn]; int Layer[maxn]; bool countLayer(int s, int t) { memset(Layer, 0, sizeof(Layer)); int id = 0, front = 0, now, i; Layer[s] = 1; queue[id++] = s; while(front < id) { now = queue[front++]; for(i = s; i <= t; ++i) if(G[now][i] && !Layer[i]) { Layer[i] = Layer[now] + 1; if(i == t) return true; else queue[id++] = i; } } return false; } // 源点,汇点,源点编号必须最小,汇点编号必须最大 int Dinic(int s, int t) { int minCut, pos, maxFlow = 0; int i, id = 0, u, v, now; while(countLayer(s, t)) { memset(vis, 0, sizeof(vis)); vis[s] = true; queue[id++] = s; while(id) { now = queue[id - 1]; if(now == t) { minCut = inf; for(i = 1; i < id; ++i) { u = queue[i - 1]; v = queue[i]; if(G[u][v] < minCut) { minCut = G[u][v]; pos = u; } } maxFlow += minCut; for(i = 1; i < id; ++i) { u = queue[i - 1]; v = queue[i]; G[u][v] -= minCut; G[v][u] += minCut; } while(queue[id - 1] != pos) vis[queue[--id]] = false; } else { for(i = s; i <= t; ++i) { if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) { vis[i] = 1; queue[id++] = i; break; } } if(i > t) --id; } } } return maxFlow; }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/yxwkf/p/4757180.html