ZOJ 2532 Internship

Internship

Time Limit: 5000ms
Memory Limit: 32768KB
This problem will be judged on ZJU. Original ID: 2532
64-bit integer IO format: %lld      Java class name: Main
 

CIA headquarter collects data from across the country through its classified network. They have been using optical fibres long before it's been deployed on any civilian projects. However they are still under a lot pressure recently because the data are growing rapidly. As a result they are considering upgrading the network 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. And as a CIA intern it's your responsibility to investigate which segment could actually help increase the total bandwidth the headquarter receives, suppose that all the cities have infinite data to send and the routing algorithm is optimized. As they have prepared the data for you in a few minutes, you are told 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, they represent the number of cities, the number of relay stations and the number of segments. Cities will be referred to as integers from 1 to n, while relay stations use integers from n+1 to n+m. You can saves assume that n + m <= 100, l <= 1000 (all of them are positive). The headquarter is identified by the integer 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 is the 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 can be 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 line and sorted in ascending order, with a single space as the separator. If none of the segment meets the criteria, just print an empty line. The segment id is 1 based not 0 based.

Sample Input
2 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 0
Sample Output
2 3
<hey here is an invisible empty line>

Source

 
解题:求关键边。。也就是最小割
 1 #include<bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 using namespace std;
 4 const int maxn = 210;
 5 struct arc{
 6     int to,flow,next;
 7     arc(int x = 0,int y = 0,int z = -1){
 8         to = x;
 9         flow = y;
10         next = z;
11     }
12 }e[maxn*maxn];
13 int head[maxn],cur[maxn],color[maxn],d[maxn],tot,S,T;
14 void add(int u,int v,int flow){
15     e[tot] = arc(v,flow,head[u]);
16     head[u] = tot++;
17     e[tot] = arc(u,0,head[v]);
18     head[v] = tot++;
19 }
20 bool bfs(){
21     queue<int>q;
22     memset(d,-1,sizeof(d));
23     d[S] = 0;
24     q.push(S);
25     while(!q.empty()){
26         int u = q.front();
27         q.pop();
28         for(int i = head[u]; ~i; i = e[i].next){
29             if(e[i].flow && d[e[i].to] == -1){
30                 d[e[i].to] = d[u] + 1;
31                 q.push(e[i].to);
32             }
33         }
34     }
35     return d[T] > -1;
36 }
37 int dfs(int u,int low){
38     if(u == T) return low;
39     int tmp = 0,a;
40     for(int &i = cur[u]; ~i; i = e[i].next){
41         if(e[i].flow && d[e[i].to] == d[u] + 1&&(a=dfs(e[i].to,min(low,e[i].flow)))){
42             e[i].flow -= a;
43             e[i^1].flow += a;
44             low -= a;
45             tmp += a;
46             if(!low) break;
47         }
48     }
49     if(!tmp) d[u] = -1;
50     return tmp;
51 }
52 int dinic(){
53     int ans = 0;
54     while(bfs()){
55         memcpy(cur,head,sizeof(head));
56         ans += dfs(S,INF);
57     }
58     return ans;
59 }
60 void dfs2(int u,int v){
61     color[u] = v;
62     for(int i = head[u]; ~i; i = e[i].next){
63         if(v == 1 && e[i].flow && !color[e[i].to]) dfs2(e[i].to,v);
64         if(v == 2 && e[i^1].flow && !color[e[i].to]) dfs2(e[i].to,v);
65     }
66 }
67 int main(){
68     int N,M,L,x[1010],y[1010],tmp;
69     while(scanf("%d %d %d",&N,&M,&L),N||M||L){
70         memset(head,-1,sizeof(head));
71         memset(color,0,sizeof(color));
72         tot = T = 0;
73         S = N + M + 1;
74         for(int i = 1; i <= N; ++i) add(S,i,INF);
75         for(int i = 0; i < L; ++i){
76             scanf("%d %d %d",x+i,y+i,&tmp);
77             add(x[i],y[i],tmp);
78         }
79         dinic();
80         dfs2(S,1);
81         dfs2(T,2);
82         bool flag = false;
83         for(int i = 0; i < L; ++i){
84             if(color[x[i]] == 1 && color[y[i]] == 2){
85                 if(flag) putchar(' ');
86                 printf("%d",i+1);
87                 flag = true;
88             }
89         }
90         putchar('\n');
91     }
92     return 0;
93 }
View Code

 

posted @ 2015-03-14 19:21  狂徒归来  阅读(216)  评论(0编辑  收藏  举报