poj 1459 Power Network 最大流
这题没有给出源点和汇点,需要自己添加。
添加一个与所有类型为p的点相连的源点和一个与所有类型为c的点相连的汇点,边的值为题目给的,而不是INF。
例外这题输入比较麻烦。
#include <iostream> #include <queue> using namespace std; const int N = 505; const int INF = 1000000000; int cap[N][N]; int flow[N][N]; int min_flow[N]; int pre[N]; int n, np, nc, m; int max_flow(int source, int sink) { memset(flow, 0, sizeof(flow)); int u; int ans = 0; while (1) { memset(pre, -1, sizeof(pre)); queue<int> Q; min_flow[source] = INF; Q.push(source); while (!Q.empty()) { u = Q.front(); Q.pop(); if (u == sink) break; for (int i = 1; i <= sink; i++) { if (pre[i] < 0 && cap[u][i] > flow[u][i]) { pre[i] = u; min_flow[i] = min(min_flow[u], cap[u][i] - flow[u][i]); Q.push(i); } } } if (pre[sink] < 0) break; for (int v = sink; v != source; v = pre[v]) { int u = pre[v]; flow[u][v] += min_flow[sink]; flow[v][u] -= min_flow[sink]; } ans += min_flow[sink]; } return ans; } int main() { int x, y, z; char c; while (scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF) { memset(cap, 0, sizeof(cap)); for (int i = 0; i < m; i++) { while (c = getchar()) { if (c == '(') break; } scanf("%d,%d)%d", &x, &y, &z); cap[x+1][y+1] = z; } for (int i = 0; i < np; i++) { while (c = getchar()) { if (c == '(') break; } scanf("%d)%d", &x, &z); cap[0][x+1] = z; } for (int i = 0; i < nc; i++) { while (c = getchar()) { if (c == '(') break; } scanf("%d)%d", &x, &z); cap[x+1][n+1] = z; } printf("%d\n", max_flow(0, n+1)); } return 0; }