Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
看似很平常的一个最大流。。怎么不过哦。。呵呵#include <stdio.h> #include <string.h> const int N = 210; const int MAXINT = 2000000000; int m, n; int c[N][N], f[N][N], fa[N], q[N], S, T; void init() { int i, u, v, w; //initiation memset(c, 0, sizeof(c)); memset(f, 0, sizeof(f)); //input for(i = 0; i<m; i++) { scanf("%d %d %d", &u, &v, &w); if(u == v) continue; c[u][v] += w; } //pretreatment } void work() { int i; int ans = 0; //最大流 do { int qs = 0, qe = 1; q[0] = S; memset(fa, 0, sizeof(fa)); while(fa[T] == 0 && qs < qe) { int cur = q[qs]; for(i = 2; i <= T; i++) { if(fa[i] == 0) { if(f[cur][i] < c[cur][i]) { fa[i] = cur; q[qe++] = i; } else if(f[i][cur] > 0) { fa[i] = -cur; q[qe++] = i; } }//if }//for qs++; }//while if(fa[T] != 0) { int minf = MAXINT; i = T; while(i != S) { int& cur = fa[i]; if(cur > 0) { if(c[cur][i] - f[cur][i] < minf) minf = c[cur][i] - f[cur][i]; i = cur; } else { if(f[i][cur] < minf) minf = f[i][cur]; i = -cur; } } ans += minf; //修正网络流 int i = T; while(i != S) { int& cur = fa[i]; if(cur > 0) { f[cur][i] += minf; i = cur; } else { f[cur][i] -= minf; i = -cur; } } }//if }while(fa[T] != 0); printf("%d\n", ans); } int main() { // freopen("t.in", "r", stdin); while(scanf("%d %d", &m, &n)!=EOF) { S = 1; T = n; init(); work(); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator