Network flow HDU 3549 flow problem
Title: http://acm.hdu.edu.cn/showproblem.php?pid=3549
Using the augmented path algorithm to solve the problem, there are two points to note:
1. Each time the augmentation, the reverse flow will be more line, at first did not notice, WA a few times ORZ
2. For the input data, the capacity should be updated incrementally.
Adjacency Matrix Storage # include <bits/stdc++.h>using namespace std;const int INF = 0x7fffffff;const int M = 15+5;int C[m][m]; Capacityint F[m][m]; Flowint Flow[m]; Current Flowsint p[m]; path//augmented path algorithm long long maxflow (int n) {long Long cnt = 0; while (true) {queue<int> q; Q.push (1); memset (flow, 0, sizeof (flow)); FLOW[1] = INF; while (!q.empty ()) {int u = q.front (); Q.pop (); for (int v=1; v<=n; ++v) {if (U = v && c[u][v] > F[u][v] &&!flow[v]/* Avoid Non-loopback */) {Q.push (v); Flow[v] = min (Flow[u], c[u][v]-f[u][v]); P[V] = u; }}} if (flow[n]==0) break; No augmentation, exit//For augmented//cout<<flow[n]<<endl; for (int v=n; v! = 1; V=p[v]) {F[p[v]][v] + = Flow[n]; F[v][p[v]]-= flow[n]; Reverse update, there is inflow, outflow naturally should also increase! WA a few times to find Orz} cnt + = Flow[n]; } return CNT;} int main (void) {//freopen ("In.txt", "R", stdin); int icase = 1; int t = 0; cin>>t; while (t--) {memset (c, 0, sizeof (c)); memset (f, 0, sizeof (f)); int n, m; scanf ("%d%d", &n, &m); for (int i=0; i<m; ++i) {int s,e,v; scanf ("%d%d%d", &s, &e, &v); C[s][e] + = V; Incoming capacity to accumulate}//special case Direct judgment if (n <= 1 | | m < 1) {printf ("Case%d:0\n", icase ++); Continue A long long cnt = Maxflow (n); printf ("Case%d:%lld\n", icase++, CNT); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Http://blog.csdn.net/core__code
Network flow HDU 3549 flow problem