答案为
#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,j,k) for(i=j;i<=k;++i)
using namespace std;
const int inf = 0x3f3f3f3f, N = 60000, M = 400005;
int level[N], cnt, cur[N], v[M], p[M], h[N], q[M], s, t, w[M];
void add(int a, int b, int c) {
p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt;
p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt;
}
bool bfs() {
int f = 0, r = 0, u, i;
memset(level, -1, sizeof level);
q[r++] = s; level[s] = 1;
while (f < r) {
u = q[f++];
for (i = h[u]; i; i = p[i]) {
if (w[i] && level[v[i]] == -1) {
level[v[i]] = level[u] + 1;
q[r++] = v[i];
}
}
}
return level[t] != -1;
}
int dfs(int u, int low) {
int i, tmp = 0; int res = 0;
if (u == t) return low;
for (i = cur[u]; i && res < low; i = p[i]) {
if (w[i] && level[v[i]] == level[u] + 1) {
tmp = dfs(v[i], min(w[i], low - res));
w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
if (w[i]) cur[u] = i;
}
}
if (!res) level[u] = -1;
return res;
}
int dinic() {
int ans = 0;
while (bfs()) {
for (int i = s; i <= t; ++i) cur[i] = h[i];
ans += dfs(s, inf);
}
return ans;
}
int main() {
int i, a, b, c, n, m, ss;
while (scanf("%d%d", &n, &m) == 2) {
s = 0; t = n + m + 1; ss = 0; cnt = 1; memset(h, 0, sizeof h);
FOR(i,1,n) scanf("%d", &a), add(s, i, a);
FOR(i,1,m) {
scanf("%d%d%d", &a, &b, &c);
ss += c; add(n + i, t, c);
add(a, n + i, inf); add(b, n + i, inf);
}
printf("%d\n", ss - dinic());
}
return 0;
}
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others)
Total Submission(s): 2252 Accepted Submission(s): 956
A famous mobile communication company is planning to build a new set of base stations. According to the previous investigation, n places are chosen as the possible new locations to build those new stations. However, the condition of each position varies much, so the costs to built a station at different places are different. The cost to build a new station at the ith place is Pi (1<=i<=n).
When complete building, two places which both have stations can communicate with each other.
Besides, according to the marketing department, the company has received m requirements. The ith requirement is represented by three integers Ai, Bi and Ci, which means if place Ai and Bi can communicate with each other, the company will get Ci profit.
Now, the company wants to maximize the profits, so maybe just part of the possible locations will be chosen to build new stations. The boss wants to know the maximum profits.
Multiple test cases (no more than 20), for each test case:
The first line has two integers n (0
One integer each case, the maximum profit of the company.
5 5
1 2 3 4 5
1 2 3
2 3 4
1 3 3
1 4 2
4 5 3
4
liulibo
2011 Multi-University Training Contest 5 - Host by BNU
lcy | We have carefully selected several similar problems for you: 1532 3572 3081 3549 3338