很明显的网络流问题,点数和边数都是200,采用Edmonds_karp的复杂度上限是O(VE^2),实际上一般情况下要比此上限小得多,所以N=200时也能很快解决
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <map>
- using namespace std;
- /*
- PROG: ditch
- LANG: C++
- ID: heben991
- */
- const int N = 210, INF = 2000000000, MaxF = 10000000;
- int g[N][N], f[N][N], inc[N], pre[N], q[N];
- int v, e;
- bool bfs(int src, int des)
- {
- int fp, rp, i, u;
- for(i = 1; i <= v; ++i)
- {
- pre[i] = -1;
- }
- q[fp=rp=0] = src;
- inc[src] = INF;
- while(fp <= rp)
- {
- u = q[fp++];
- for(i = 1; i <= v; ++i)
- if(pre[i]==-1 && f[u][i] < g[u][i])
- {
- inc[i] = min(inc[u], g[u][i]-f[u][i]);
- pre[i] = u;
- if(i == des) return true;
- q[++rp] = i;
- }
- }
- return false;
- }
- int Edmonds_karp(int src, int des)
- {
- int i, j;
- int maxflow = 0;
- while( bfs(src,des) )
- {
- maxflow += inc[des];
- for(i = des; i != src; i = pre[i])
- {
- f[pre[i]][i] += inc[des];
- f[i][pre[i]] = -f[pre[i]][i];
- }
- }
- return maxflow;
- }
- int main()
- {
- int i, j, k, x, y;
- freopen("ditch.in", "r", stdin);
- freopen("ditch.out","w",stdout);
- scanf("%d%d", &e, &v);
- for(i = 1; i <= e; ++i)
- {
- scanf("%d%d%d", &x, &y, &k);
- g[x][y] += k;
- }
- printf("%d/n", Edmonds_karp(1,v) );
- return 0;
- }