Description

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.

题目大意:有n个发射点,m个中间点,和一个接受点0,中间由L条有向光纤连接,每个光纤有一定的带宽,问扩大哪些光纤(只能扩大一条)的带宽可以扩大发射点的总带宽。

思路:从源点S到n个发射点连一条容量为无穷大的边,再连接L条光纤,容量为该光纤的带宽,以0为汇点T。题目转化成扩大哪些边的容量可以增加最大流。先在图上求最大流,在残量网络中,把S能到的点标记为1,把能到T的点标记为2,那么当一条边的出发点为1、结束点为2,这条边就是关键割边,也就是所求的边之一。如果有一条边的其中一个点未被标记,那么扩大这条边的容量,也就不过是从源点到达的点多了一个,或者到达汇点的点多了一个,但是在这个残量网络中依旧没有从S到T的增广路。如果两个点都未被标记……那更加不可以啦……

PS:出发点为2结束点为1的边,增加容量也不会有增广路出现,曾因为这个WA了一次>_<

BFS+ISAP(30MS):

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = ;
const int INF = 0x3fff3fff; struct SAP {
int head[MAXN], dis[MAXN], pre[MAXN], cur[MAXN], gap[MAXN];
int to[MAXE], next[MAXE], flow[MAXE];
int n, st, ed, ecnt; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; flow[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
//printf("%d->%d flow = %d\n", u, v, c);
} void bfs() {
memset(dis, 0x3f, sizeof(dis));
queue<int> que; que.push(ed);
dis[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
++gap[dis[u]];
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p ^ ] && dis[v] > n) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int Max_flow(int ss, int tt, int nn) {
st = ss; ed = tt; n = nn;
int ans = , minFlow = INF, u;
for(int i = ; i <= n; ++i) {
cur[i] = head[i];
gap[i] = ;
}
u = pre[st] = st;
bfs();
while(dis[st] < n) {
bool flag = false;
for(int &p = cur[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[u] == dis[v] + ) {
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed) {
ans += minFlow;
while(u != st) {
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow;
}
minFlow = INF;
}
break;
}
}
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && minDis > dis[v]) {
minDis = dis[v];
cur[u] = p;
}
}
if(--gap[dis[u]] == ) break;
gap[dis[u] = minDis + ]++;
u = pre[u];
}
return ans;
} int mark[MAXN]; void make_cut() {
memset(mark, , sizeof(mark));
queue<int> que;
que.push(st); mark[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && !mark[v]) {
mark[v] = ;
que.push(v);
}
}
}
que.push(ed); mark[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p ^ ] && !mark[v]) {
mark[v] = ;
que.push(v);
}
}
}
}
} G; int from[MAXE], to[MAXE];
int n, m, L, c; int main() {
while(scanf("%d%d%d", &n, &m, &L) != EOF) {
if(n == ) break;
int ss = n + m + , tt = ;
G.init();
for(int i = ; i <= n; ++i) G.add_edge(ss, i, INF);
for(int i = ; i <= L; ++i) {
scanf("%d%d%d", &from[i], &to[i], &c);
G.add_edge(from[i], to[i], c);
}
//printf("%d\n", G.Max_flow(ss, tt, ss));
G.Max_flow(ss, tt, ss);
G.make_cut();
bool flag = false;
for(int i = ; i <= L; ++i) {
int &u = from[i], &v = to[i];
if(G.mark[u] == && G.mark[v] == ) {
if(flag) printf(" ");
printf("%d", i);
flag = true;
}
}
puts("");
}
}

最新文章

  1. 如何dos命令打开服务窗口?
  2. centos yum Segmentation fault 问题解决办法
  3. MEF入门之不求甚解,但力求简单能讲明白(四)
  4. VS2013-解决error C4996: &#39;fopen&#39;问题
  5. 团队项目--站立会议 DAY3
  6. Halcon标定步骤
  7. fastreport totalpage 只有设置doublepassreport为true 才正确否则为0
  8. 使用javabean连接数据库时遇到的问题
  9. php发送ssl邮件
  10. windows server 2008 r2电脑历史操作记录
  11. android studio 偶记
  12. 获取当前 系统时间 + 获取当前URL 键值;
  13. 大白话5分钟带你走进人工智能-第三节最大似然推导mse损失函数(深度解析最小二乘来源)(1)
  14. 从0开始的Python学习018更多的Python内容
  15. 前端基础之JavaScript - day14
  16. 使用python脚本实现iOS图片资源压缩
  17. 【python深入】dict和list实现排序:sorted()和lambda的使用
  18. 洛谷P4332 [SHOI2014]三叉神经树(LCT,树剖,二分查找,拓扑排序)
  19. 在nginx中,禁止IP访问.只可以使用域名访问.
  20. mongo学习使用记录1

热门文章

  1. 『ACM C++』 PTA 天梯赛练习集L1 | 050-51
  2. PHP中级程序员常见面试题
  3. ubuntu系统的软件包管理工具
  4. Spring总结以及在面试中的一些问题
  5. 『Python基础-7』for循环 &amp; while循环
  6. 使用gogs和glide来轻松拉取golang第三方库
  7. C#中访问私有成员--反射
  8. day 6 老王开枪打人
  9. mysql 题目练习
  10. Android AOSP 单独编译某一模块