CIA headquarter collects data from across the country through its classified network. They have been usingoptical fibres long before it's been deployed on any civilian projects. However they are still under a lotpressure recently because the data are growing rapidly. As a result they are considering upgrading thenetwork with new technologies that provide a few times wider bandwidth. In the experiemental stage,they would like to upgrade one segment of their original network in order to see how it performs. Andas a CIA intern it's your responsibility to investigate which segment could actually help increasethe total bandwidth the headquarter receives, suppose that all the cities have infinite data to sendand the routing algorithm is optimized. As they have prepared the data for you in a few minutes, you aretold that they need the result immediately. Well, practically immediately.
Input
Input contains multiple test cases. First line of each test case contains three integers n, m and l, theyrepresent the number of cities, the number of relay stations and the number of segments. Cities will bereferred to as integers from 1 to n, while relay stations use integers from n+1 to n+m. You can savesassume that n + m <= 100, l <= 1000 (all of them are positive). The headquarter is identified by theinteger 0.
The next l lines hold a segment on each line in the form of a b c, where a is the source node and b isthe target node, while c is its bandwidth. They are all integers where a and b are valid identifiers(from 0 to n+m). c is positive. For some reason the data links are all directional.
The input is terminated by a test case with n = 0. You can safely assume that your calculation canbe housed within 32-bit integers.
Output
For each test print the segment id's that meets the criteria. The result is printed in a single lineand sorted in ascending order, with a single space as the separator. If none of the segment meets thecriteria, just print an empty line. The segment id is 1 based not 0 based.
Sample Input2 1 3 1 3 2 3 0 1 2 0 1 2 1 3 1 3 1 2 3 1 3 0 2 0 0 0Sample Output
2 3 <hey here is an invisible empty line>
和之前那个poj 3204做法一样。。。。不在解释了。。唯一不同的是这里建一个超级源点。。。建的时候把s和1到n各建一个inf容量的边(在这里,我刚开始建的是入度为零的边,感觉应该是对的,各种wa。。。至今不明白什么原因。。难不成会有那样的数据中转站入度为0???? )。。。。然后建完图就跟之前那个题做法一样了。。。AC。。。。。嗨皮。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxM 50000
#define maxN 200
#define inf 100000000
#define CC(m,v) memset(m,v,sizeof(m))
struct node {
int u, v, f, num, next;
} edge1[maxM], edge2[maxM];
int head1[maxN], p, head2[maxN];
int que[maxN], lev[maxN], cur[maxN];
int vis1[maxN], vis2[maxN], ans[maxN];
inline void init1() {
p = 0, CC(head1, -1), CC(head2, -1);
}
void addedge(int u, int v, int f, int i) {
edge1[p].u = u, edge1[p].v = v, edge1[p].f = f, edge1[p].next = head1[u];
edge1[p].num = i, head1[u] = p;
edge2[p].u = u, edge2[p].v = v, edge2[p].f = 0, edge2[p].next = head2[u];
edge2[p].num = i, head2[u] = p++;
edge1[p].u = v, edge1[p].v = u, edge1[p].f = 0, edge1[p].next = head1[v];
edge1[p].num = i, head1[v] = p;
edge2[p].u = v, edge2[p].v = u, edge2[p].f = f, edge2[p].next = head2[v];
edge2[p].num = i, head2[v] = p++;
}
bool bfs(int s, int t) {
int fir, en, u, v, i;
memset(lev, 0, sizeof (lev));
lev[s] = 1, que[0] = s, fir = 0, en = 1;
while (fir != en) {
u = que[fir++];
for (i = head1[u]; i != -1; i = edge1[i].next)
if (edge1[i].f > 0 && lev[v = edge1[i].v] == 0) {
lev[v] = lev[u] + 1, que[en++] = v;
if (v == t) {
fir = en;
break;
}
}
}
return lev[t];
}
int dinic(int s, int t) {
int u, j, k, iq, f;
int flow = 0;
while (bfs(s, t)) {
memcpy(cur, head1, sizeof (head1));
u = s, iq = 0;
while (1) {
if (u == t) {
for (k = 0, f = inf; k < iq; k++)
if (edge1[que[k]].f < f)
f = edge1[que[k]].f, j = k;
for (k = 0; k < iq; k++) {
edge1[que[k]].f -= f, edge1[que[k]^1].f += f;
edge2[que[k]].f += f, edge2[que[k]^1].f -= f;
}
flow += f, u = edge1[que[iq = j]].u;
}
for (j = cur[u]; cur[u] != -1; j = cur[u] = edge1[cur[u]].next)
if (edge1[j].f > 0 && lev[u] + 1 == lev[edge1[j].v]) break;
if (cur[u] != -1) {
que[iq++] = cur[u];
u = edge1[cur[u]].v;
} else {
if (iq == 0) break;
lev[u] = -1;
u = edge1[que[--iq]].u;
}
}
}
return flow;
}
void dfs1(int u) {
vis1[u] = 1;
for (int i = head1[u]; i != -1; i = edge1[i].next)
if (edge1[i].f > 0 && vis1[edge1[i].v]==0)
dfs1(edge1[i].v);
}
void dfs2(int u) {
vis2[u] = 1;
for (int i = head2[u]; i != -1; i = edge2[i].next)
if (edge2[i].f > 0 && vis2[edge2[i].v]==0)
dfs2(edge2[i].v);
}
int main() {
int n, m, len, i, u, v, f, s, t, j;
while (scanf("%d%d%d", &n, &m, &len) && (n || m || len)) {
init1();
s = n + m + 1, t = 0;
for (i = 1; i <= len; i++) {
scanf("%d%d%d", &u, &v, &f);
addedge(u, v, f, i);
}
for (j = 1; j <=n; j++)
addedge(s, j, inf, -1);
dinic(s, t);
CC(vis1, 0), CC(vis2, 0);
dfs1(s), dfs2(t);
for (i = 0, j = 0; i < p; i+=2) //跟上一个一样重点强调要+=2。。。
if (edge1[i].f == 0 && vis1[edge1[i].u] && vis2[edge1[i].v] && (edge1[i].num != -1)) {
ans[j++] = edge1[i].num;
}
if (j == 0) {
printf("\n");
continue;
}
sort(ans, ans + j);
for (i = 0; i < j - 1; i++)
printf("%d ", ans[i]);
printf("%d\n", ans[j - 1]);
}
return 0;
}