Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 56870 | Accepted: 21863 |
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
#include <stdio.h> #include <string.h> #include <queue> #define maxn 202 #define inf 0x7fffffff using std::queue; int G[maxn][maxn], Layer[maxn]; int Sta[maxn]; bool vis[maxn]; bool countLayer(int m) { queue<int> Q; Q.push(1); memset(Layer, 0, sizeof(int) * (m + 1)); int i, now; Layer[1] = 1; while(!Q.empty()) { now = Q.front(); Q.pop(); for(i = 1; i <= m; ++i) { if(G[now][i] && !Layer[i]) { Layer[i] = Layer[now] + 1; if(m == i) return true; else Q.push(i); } } } return false; } int Dinic(int m) { int minCut, minCutPos, maxFlow = 0; int s, t, i, id = 0, now; while(countLayer(m)) { memset(vis, 0, sizeof(bool) * (m + 1)); vis[1] = 1; Sta[id++] = 1; while(id) { //栈中需要存放路径,所以不出栈 now = Sta[id - 1]; if(now == m) { minCut = inf; for(i = 1; i < id; ++i) { s = Sta[i - 1]; t = Sta[i]; if(G[s][t] && G[s][t] < minCut) { minCut = G[s][t]; minCutPos = s; } } //增广,添加取消流 maxFlow += minCut; for(i = 1; i < id; ++i) { s = Sta[i - 1]; t = Sta[i]; G[s][t] -= minCut; G[t][s] += minCut; } //出栈到最小割位置 while(id && Sta[id - 1] != minCutPos) { vis[Sta[--id]] = 0; } } else { for(i = 1; i <= m; ++i) { if(G[now][i] && !vis[i] && Layer[now] + 1 == Layer[i]) { Sta[id++] = i; vis[i] = 1; break; } } if(i > m) --id; } } } return maxFlow; } int main() { //freopen("stdin.txt", "r", stdin); int n, m, i, s, t, c; while(scanf("%d%d", &n, &m) == 2) { memset(G, 0, sizeof(G)); for(i = 0; i < n; ++i) { scanf("%d%d%d", &s, &t, &c); G[s][t] += c; } printf("%d\n", Dinic(m)); } return 0; }
2015.04.20
#include <stdio.h> #include <string.h> const int maxn = 210; const int maxm = 210; const int inf = 0x3f3f3f3f; int N, M; int head[maxn], id; struct Node { int u, v, c, next; } E[maxm<<1]; // 参数:顶点个数(若编号0不用则传入最大编号+1),源点,汇点 int dep[maxn], ps[maxn], cur[maxn]; int Dinic(int n, int s, int t) { int tr, res = 0; int i, j, k, f, r, top; while(true) { memset(dep, -1, n * sizeof(int)); for(f = dep[ps[0] = s] = 0, r = 1; f != r; ) for(i = ps[f++], j = head[i]; j != -1; j = E[j].next) { if(E[j].c && -1 == dep[k=E[j].v]) { dep[k] = dep[i] + 1; ps[r++] = k; if(k == t) { f = r; break; } } } if(-1 == dep[t]) break; memcpy(cur, head, n * sizeof(int)); for(i = s, top = 0; ; ) { if(i == t) { for(k = 0, tr = inf; k < top; ++k) if(E[ps[k]].c < tr) tr = E[ps[f=k]].c; for(k = 0; k < top; ++k) E[ps[k]].c -= tr, E[ps[k]^1].c += tr; res += tr; i = E[ps[top = f]].u; } for(j = cur[i]; cur[i] != -1;j = cur[i] = E[cur[i]].next) if(E[j].c && dep[i] + 1 == dep[E[j].v]) break; if(cur[i] != -1) { ps[top++] = cur[i]; i = E[cur[i]].v; } else { if(0 == top) break; dep[i] = -1; i = E[ps[--top]].u; } } } return res; } void init() { memset(head, -1, sizeof(head)); id = 0; } void addEdge(int u, int v, int c) { E[id].u = u; E[id].v = v; E[id].c = c; E[id].next = head[u]; head[u] = id++; E[id].u = v; E[id].v = u; E[id].c = 0; E[id].next = head[v]; head[v] = id++; } void getMap() { int u, v, c, i; while (N--) { scanf("%d%d%d", &u, &v, &c); addEdge(u, v, c); } } int solve() { printf("%d\n", Dinic(M + 1, 1, M)); } int main() { while (~scanf("%d%d", &N, &M)) { init(); getMap(); solve(); } return 0; }