uva 10330 - Power Transmission
题目大意:最大流问题。
解题思路:増广路算法。
#include <stdio.h>
#include <string.h>
#include <queue> using namespace std; #define min(a,b) (a)<(b)?(a):(b) const int N = 105;
const int INF = 0x3f3f3f3f; int n, s[N], g[N][N], f[N][N]; void init() {
memset(s, 0, sizeof(s));
memset(g, INF, sizeof(g));
memset(f, INF, sizeof(f)); for (int i = 1; i <= n; i++)
scanf("%d", &s[i]); int a, b, c, t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%d%d%d", &a, &b, &c);
g[a][b] = min(g[a][b], c);
f[a][b] = 0;
} scanf("%d%d", &a, &b);
for (int i = 0; i < a; i++) {
scanf("%d", &c);
g[0][c]--;
f[0][c] = 0;
} for (int i = 0; i < b; i++) {
scanf("%d", &c);
g[c][n + 1]--;
f[c][n + 1] = 0;
}
} int solve() {
n++;
s[n] = INF;
int a[N], vis[N];
int ans = 0;
queue<int> q; while (1) { memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a)); int c = 0, t; vis[c] = 0;
a[c] = INF;
q.push(c); while (!q.empty()) { c = q.front(), q.pop(); for (int i = 1; i <= n; i++) { if (g[c][i] == INF) continue;
t = min(g[c][i] - f[c][i], min(a[c], s[i])); if (t > a[i]) {
vis[i] = c;
a[i] = t;
q.push(i);
}
}
} if (a[n] == 0) break;
ans += a[n]; for (int i = n; i; i = vis[i]) { s[i] -= a[n];
f[vis[i]][i] += a[n];
}
}
return ans;
} int main () { while (scanf("%d", &n) == 1) { init();
printf("%d\n", solve());
}
return 0;
}