2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
Case 1: 1 Case 2: 2
純求網絡最大流,用EK算法就可以實現。
代碼:
#include#include #include #include using namespace std; #define maxn 20 #define INF 0x3f3f3f3f int ans, s, t, n; int a[maxn], pre[maxn]; int flow[maxn][maxn]; int cap[maxn][maxn]; void Edmonds_Karp() { queue q; memset(flow, 0, sizeof(flow)); ans = 0; while(1) { memset(a, 0, sizeof(a)); a[s] = INF; q.push(s); while(!q.empty()) //bfs找增光路徑 { int u = q.front(); q.pop(); for(int v = 1; v <= n; v++) if(!a[v] && cap[u][v] > flow[u][v]) { pre[v] = u; q.push(v); a[v] = min(a[u], cap[u][v]-flow[u][v]); } } if(a[t] == 0) break; for(int u = t; u != s; u = pre[u]) //改進網絡流 { flow[pre[u]][u] += a[t]; flow[u][pre[u]] -= a[t]; } ans += a[t]; } } int main() { //freopen(hdu_3549.txt, r, stdin); int T, m, u, v, c; scanf(%d, &T); for(int cas = 1; cas <= T; cas++) { scanf(%d%d, &n, &m); memset(cap, 0, sizeof(cap)); while(m--) { scanf(%d%d%d, &u, &v, &c); cap[u][v] += c; } s = 1, t = n; Edmonds_Karp(); printf(Case %d: %d , cas, ans); } return 0; }