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

CYJJ‘s Funny Contest #3, Having Fun in Summer

解题:求关键边。。也就是最小割

 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 }

时间: 2024-10-13 07:35:26

ZOJ 2532 Internship的相关文章

ZOJ 2532 Internship 求隔边

Internship Time Limit: 5 Seconds      Memory Limit: 32768 KB 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 the

ZOJ 1532 Internship (Dinic)

看来模板又错了,敲你妈妈小饼干 #include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<stack> #include<vector> #include<map> using na

网络流(进阶)

最大流:DINIC or SAP 最小费用最大流:SPFA+增广(费用的值较离散) or ZKW(费用的值集中) 有源汇的上下界最大流:新建s', t',用(i, j, l, r)表示i到j有一条下界为l上界为r的边,将每条这样的边拆成(s', j, 0, l), (i, t', 0, l), (i, j, 0, r-l),加入边(t, s, 0, max)再从s'到t'求最大流,再去掉(t, s, 0, max)这条边,从s到t求最大流 有源汇的上下界最小可行流:基本同上,将最后一步改成从t到

[转] 一些图论、网络流入门题总结、汇总

最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http://ac

【转】一些图论、网络流入门题总结、汇总

最短路问题 此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等) http://acm.pku.edu.cn/JudgeOnline/problem?id=2449 题意:经典问题:K短路 解法:dijkstra+A*(rec),方法很多 相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144 该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础) ht

网络流专栏

最大流 POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall (二分图匹配) POJ 1698 Alice's Chance(构图) POJ 1459 Power Network(构图) POJ 2112 Optimal Milking (二分) POJ 2455 Secret Milking Machine (二分) POJ 3189 Steady Cow Assignment (枚举) POJ 1637 Sightseeing tour (

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

[转载] 一些图论、网络流入门题总结、汇总

转载 最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http:/

图论常用算法之一 POJ图论题集【转载】

POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:http://poj.org/ 1062* 昂贵的聘礼 枚举等级限制+dijkstra 1087* A Plug for UNIX 2分匹配 1094 Sorting It All Out floyd 或 拓扑 1112* Team Them Up! 2分图染色+DP 1125 Stockbroker