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 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 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>
Author: WU, Jiazhi

题意

CIA公司想采用新技术升级网络,在实验测试阶段,他们想升级其中的一段网络以便观察新技术在多大的长度上提升网络的性能,你作为实习生的任务是调查那一段网络能提高CIA总部的宽带。

思路

找割边集。 
判断一段网络可不可以提升网络就要看它是不是满流,如果满流则可能在升级后提升CIA总部的宽带,但是如果提升后并不能增广,即不能提升CIA总部的宽带,所以判断一段是不是可提升的则有两个条件:(1)在进行增广后这段网络是满流的,(2)在提升后可以增广。

所以从源DFS一次,标记,从汇DFS一次,标记。再枚举边,判断。

以上内容来自  https://blog.csdn.net/xzxxzx401/article/details/78313184

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <set>
  7 #include <iostream>
  8 #include <map>
  9 #include <stack>
 10 #include <string>
 11 #define pi acos(-1.0)
 12 #define eps 1e-6
 13 #define fi first
 14 #define se second
 15 #define lson l,m,rt<<1
 16 #define rson m+1,r,rt<<1|1
 17 #define bug      printf("******")
 18 #define mem(a,b) memset(a,b,sizeof(a))
 19 #define fuck(x)  cout<<"["<<x<<"]"<<endl
 20 #define f(a)     a*a
 21 #define san(n,m) scanf("%d%d",&n,&m)
 22 #define FIN freopen("in.txt","r",stdin)
 23 #define lowbit(x) x&-x
 24 #pragma comment (linker,"/STACK:102400000,102400000")
 25 using namespace std;
 26 const int maxn = 10004;
 27 typedef long long LL;
 28 const int MX = 10050;
 29 const int MXE = 4 * MX * MX;
 30 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
 31 const int INF = 0x3f3f3f;
 32 struct Edge {
 33     int v, u, nxt;
 34     LL w;
 35 } edge[maxn];
 36 int tot, num, s, t;
 37 int head[MX];
 38 void init() {
 39     memset (head, -1, sizeof (head) );
 40     tot = 0;
 41 }
 42 void add (int u, int v, LL w) {
 43     edge[tot].v = v;
 44     edge[tot].u = u;
 45     edge[tot].w = w;
 46     edge[tot].nxt = head[u];
 47     head[u] = tot++;
 48 }
 49 int  d[MX], vis[MX], gap[MX];
 50 void bfs() {
 51     memset (d, 0, sizeof (d) );
 52     memset (gap, 0, sizeof (gap) );
 53     memset (vis, 0, sizeof (vis) );
 54     queue<int>q;
 55     q.push (t);
 56     vis[t] = 1;
 57     while (!q.empty() ) {
 58         int u = q.front();
 59         q.pop();
 60         for (int i = head[u]; ~i; i = edge[i].nxt) {
 61             int v = edge[i].v;
 62             if (!vis[v]) {
 63                 d[v] = d[u] + 1;
 64                 gap[d[v]]++;
 65                 q.push (v);
 66                 vis[v] = 1;
 67             }
 68         }
 69     }
 70 }
 71 int last[MX];
 72 LL dfs (int u, LL f) {
 73     if (u == t) return f;
 74     LL sap = 0;
 75     for (int i = last[u]; ~i; i = edge[i].nxt) {
 76         int v = edge[i].v;
 77         if (edge[i].w > 0 && d[u] == d[v] + 1) {
 78             last[u] = i;
 79             LL tmp = dfs (v, min (f - sap, edge[i].w) );
 80             edge[i].w -= tmp;
 81             edge[i ^ 1].w += tmp;
 82             sap += tmp;
 83             if (sap == f) return sap;
 84         }
 85     }
 86     if (d[s] >= num) return sap;
 87     if (! (--gap[d[u]]) ) d[s] = num;
 88     ++gap[++d[u]];
 89     last[u] = head[u];
 90     return sap;
 91 }
 92 LL solve (int st, int ed, int n) {
 93     LL flow = 0;
 94     num = n;
 95     s = st;
 96     t = ed;
 97     bfs();
 98     memcpy (last, head, sizeof (head) );
 99     while (d[s] < num) flow += dfs (s, INFLL);
100     return flow;
101 }
102 int vis1[maxn], vis2[maxn], n, m, l, ans[maxn];
103 void dfs1(int u, int *vist, int op) {
104     vist[u] = true;
105     for(int i = head[u]; i != -1; i = edge[i].nxt) {
106         if(!vist[edge[i].v] && edge[i ^ op].w != 0) {
107             dfs1(edge[i].v, vist, op);
108         }
109     }
110 }
111
112 int main() {
113     int l;
114     while(~scanf("%d%d%d", &n, &m, &l)) {
115         init();
116         if(n + m + l == 0) break;
117         s = n + m + 1;
118         t = 0;
119         int a, b, c;
120         for(int i = 0; i < l; i++) {
121             scanf("%d %d %d", &a, &b, &c);
122             add(a, b, c);
123             add(b, a, 0);
124         }
125         for(int i = 1; i <= n; i++) {
126             add(s, i, INF);
127             add(i, s, 0);
128         }
129         solve ( s,  t, n + m + 2) ;
130         memset(vis1, false, sizeof(vis1));
131         memset(vis2, false, sizeof(vis2));
132         dfs1(s, vis1, 0);
133         dfs1(t, vis2, 1);
134         int num = 0;
135         for(int i = 0; i < l; i++) {
136             if(edge[i << 1].w == 0 && vis1[edge[i << 1].u] && vis2[edge[i << 1].v]) {
137                 ans[num++] = i + 1;
138             }
139         }
140         if(num) {
141             for(int i = 0; i < num; i++) {
142                 if(i)  printf(" ");
143                 printf("%d", ans[i]);
144             }
145         }
146         printf("\n");
147     }
148     return 0;
149 }


原文地址:https://www.cnblogs.com/qldabiaoge/p/9415814.html

时间: 2024-10-01 03:16:24

ZOJ 2532 Internship 求隔边的相关文章

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.

ZOJ 3537 Cake 求凸包 区间DP

题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价.现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价是多少. 思路:首先判断多边形是否是凸多边形,之后就是区间dp了. 求出凸包后,按逆时针来看. 设置dp[i][j]为从顶点i到顶点j所围成凸多边形的最优解. 枚举切点k (i < k < j) dp[i][j] = min(dp[i][k] + dp[k][j] + cost[i][k] + c

ZOJ 1010 Area 求任意多边形面积

主要判断是否是多边形:1.n<3 : 2.非相邻两条线段不相交 #include <iostream> #include <cmath> #include <stdio.h> using namespace std; #define eps 1e-8 int sig(double x) { if(x<-eps) return -1; if(x>eps) return 1; return 0; } struct point { double x,y; }

ZOJ 3822(求期望)

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

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 (