注意这道题是双向边, 然后直接套模板就ok了。
#include#include #include #include #include #define REP(i, a, b) for(int i = (a); i < (b); i++)using namespace std;const int MAXN = 112;struct Edge{ int from, to, cap, flow; Edge(int from = 0,int to = 0,int cap = 0,int flow = 0):from(from),to(to),cap(cap),flow(flow){}};vector edges;vector g[MAXN];int h[MAXN], cur[MAXN], s, t, n, m;void AddEdge(int from, int to, int cap){ edges.push_back(Edge(from, to, cap, 0)); edges.push_back(Edge(to, from, 0, 0)); g[from].push_back(edges.size() - 2); g[to].push_back(edges.size() - 1);}bool bfs(){ memset(h, 0, sizeof(h)); queue q; q.push(s); h[s] = 1; while(!q.empty()) { int x = q.front(); q.pop(); REP(i, 0, g[x].size()) { Edge& e = edges[g[x][i]]; if(e.cap > e.flow && !h[e.to]) { h[e.to] = h[x] + 1; q.push(e.to); } } } return h[t];}int dfs(int x, int a){ if(x == t || a == 0) return a; int flow = 0, f; for(int i = cur[x]; i < g[x].size(); i++) { Edge& e = edges[g[x][i]]; if(h[x] + 1 == h[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[g[x][i] ^ 1].flow -= f; flow += f; if((a -= f) == 0) break; } } return flow;}int solve(){ int ret = 0; while(bfs()) memset(cur, 0, sizeof(cur)), ret += dfs(s, 1e9); return ret;}int main(){ int kase = 0; while(~scanf("%d", &n) && n) { REP(i, 1, n + 1) g[i].clear(); edges.clear(); scanf("%d%d%d", &s, &t, &m); while(m--) { int u, v, f; scanf("%d%d%d", &u, &v, &f); AddEdge(u, v, f); AddEdge(v, u, f); } printf("Network %d\nThe bandwidth is %d.\n\n", ++kase, solve()); } return 0;}