求 S 到 T 的最大流量為多少。網路是雙向連接的,但共用頻寬,例如 A B 10,則 A 到 B 的流量 + B 到 A 的流量要小於等於 10。另外這題給測資的方式會有可能給你很多組 A B Xi,所以 A 到 B 的頻寬要把 Xi 全部加起來,例如底下例子 1 ~ 2 的頻寬為 30。 2 1 2 2 1 2 10 1 2 20
template <typename T> T QPOP(queue<T>& q) { T tmp = q.front(); q.pop(); return tmp; }
// reference: https://ppt.cc/fv1Gvx
constint INF = (int)1e9; constint MXN = 101;
int f[MXN][MXN]; int c[MXN][MXN]; int p[MXN]; bool vis[MXN];
int N, S, T, C; // S: Source, T: Sink
voidinit() { CLR(f); CLR(c); }
voidread() { int u, v, w; cin >> S >> T >> C; while (C--) { cin >> u >> v >> w; c[u][v] += w; c[v][u] += w; } }
intaugment(int u, int v, int bottleNeck) { if (v == S) return bottleNeck; bottleNeck = augment(p[u], u, min(c[u][v] - f[u][v], bottleNeck)); f[u][v] += bottleNeck; f[v][u] -= bottleNeck; return bottleNeck; }
// Edmonds-Karp intmaxiumFlow() { int mf = 0;
while (true) { CLR(vis);
queue<int> q; q.push(S); vis[S] = true;
while (!q.empty() && !vis[T]) { int u = QPOP(q);
FOR(v, 1, N + 1) { if (vis[v] || f[u][v] >= c[u][v]) continue;
q.push(v); vis[v] = true; p[v] = u; } }
if (!vis[T]) break; // no path mf += augment(p[T], T, INF); }
return mf; }
voidsolve() { staticint C = 0; cout << "Network " << ++C << '\n'; cout << "The bandwidth is " << maxiumFlow() << ".\n\n"; }
intmain() { while (cin >> N && N) { init(); read(); solve(); } }
template <typename T> T QPOP(queue<T>& q) { T tmp = q.front(); q.pop(); return tmp; }
// reference: https://ppt.cc/fv1Gvx
constint INF = (int)1e9; constint MXN = 101;
int rn[MXN][MXN]; int p[MXN]; bool vis[MXN];
int N, S, T, C; // S: Source, T: Sink
voidinit() { CLR(rn); }
voidread() { int u, v, w; cin >> S >> T >> C; while (C--) { cin >> u >> v >> w; rn[u][v] += w; rn[v][u] += w; } }
intaugment(int u, int v, int bottleNeck) { if (v == S) return bottleNeck; bottleNeck = augment(p[u], u, min(rn[u][v], bottleNeck)); rn[u][v] -= bottleNeck; rn[v][u] += bottleNeck; return bottleNeck; }
// Edmonds-Karp intmaxiumFlow() { int mf = 0;
while (true) { CLR(vis);
queue<int> q; q.push(S); vis[S] = true;
while (!q.empty() && !vis[T]) { int u = QPOP(q);
FOR(v, 1, N + 1) { if (vis[v] || !rn[u][v]) continue;
q.push(v); vis[v] = true; p[v] = u; } }
if (!vis[T]) break; // no path mf += augment(p[T], T, INF); }
return mf; }
voidsolve() { staticint C = 0; cout << "Network " << ++C << '\n'; cout << "The bandwidth is " << maxiumFlow() << ".\n\n"; }
intmain() { while (cin >> N && N) { init(); read(); solve(); } }