"Maximum Flow | key Edge" ZOJ-1532 Internship

Source: Internet
Author: User

Internship
Time Limit:5 Seconds Memory limit:32768 KB

CIA headquarter collects data from across, the country through its classified network. They has been using optical fibres long before it ' s been deployed on any civilian projects. However they is still under a lot pressure recently because the data is growing rapidly. As a result they is 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 perfor MS and as a CIA intern it ' s your responsibility to investigate which segment could actually help increase the total BANDW Idth The headquarter receives, suppose that all the cities has infinite data to send and the routing algorithm is Optimiz Ed. As they has prepared the data for your in a few minutes, and you is told that they need the result immediately. Well, practically immediately.

Input

Input contains multiple test cases. First line of all 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'll is referred to as integers from 1 to n and while relay stations use integers from n+1 to n+m. You can saves assume that n + M <=, L <= (all of them is 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 are the target node, WH Ile C is its bandwidth. They is all integers where a and b is valid identifiers (from 0 to n+m). C is positive. For some reason the data links is all directional.

The input is terminated by a test case with n = 0. You can safely assume this 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, 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

Test Instructions : a capacity network consisting of a headquarters, several cities, relay stations and some network segments. Each network segment is unidirectional and has a certain amount of bandwidth. Cities will upload data without restrictions, and find some of the network segments in the graph: increasing the bandwidth of these segments increases the total bandwidth received by the headquarters.
idea : Build a map--the relay station and the city are the vertices, the network segment is the edge, the headquarters is the meeting point. Create a super source point and each city edge with a capacity of INF.
To find out the key side of this capacity network 1, first to run the maximum flow, and then in the residual network, respectively, from the source and meeting point DFS and staining, if the edge is not full stream can continue Dfs. The last full stream and each end point having a different color of 2 is the key edge.
Also be sure to pay attention to the true number of edges. It should be 3000.
The code is as follows :

/* * ID:J.SURE.1 * PROG: * lang:c++ * *#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define PB push_back#define LL Long Longusing namespace STD;Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-8;/****************************************/Const intN = -+5, M =3e3+5;intN, M, L, tot, SRC, sink;structEdge {intU, V, W, next; Edge () {} Edge (int_u,int_v,int_w,int_next): U (_u), V (_v), W (_w), Next (_next) {}}e[m];intHead[n], Cur[n], s[n], lev[n];intLINE[M];BOOLVis1[n], vis2[n];voidInit () {memset(Head,-1,sizeof(head));memset(Vis1,0,sizeof(VIS1));memset(Vis2,0,sizeof(VIS2)); tot =0;}voidAddintUintVintW) {E[tot] = Edge (U, V, W, Head[u]);    Head[u] = tot++; E[tot] = Edge (V, U,0, Head[v]); HEAD[V] = tot++;}BOOLBFS () { Queue <int>Qmemset(Lev,-1,sizeof(Lev)); LEV[SRC] =0; Q.push (SRC); while(!q.empty ()) {intU = Q.front (); Q.pop (); for(inti = Head[u]; ~i; i = e[i].next) {intv = e[i].v;if(E[I].W && lev[v] = =-1) {Lev[v] = Lev[u] +1; Q.push (v);if(v = = sink)return true; }        }    }return false;}intDinic () {intRET =0; while(BFS ()) {memcpy(Cur, head,sizeof(cur));intU = src, top =0; while(1) {if(U = = sink) {intMini = INF, loc; for(inti =0; i < top; i++) {if(Mini > E[S[I]].W)                        {mini = E[S[I]].W;                    loc = i; }                } for(inti =0; i < top;                    i++) {E[S[I]].W-= mini; e[s[i]^1].W + = Mini;                } ret + = Mini;                top = loc;            u = e[s[top]].u; }int&i = Cur[u]; for(; ~i; i = e[i].next) {intv = e[i].v;if(E[I].W && lev[v] = = Lev[u] +1) Break; }if(~i)                {s[top++] = i;            u = e[i].v; }Else{if(!top) Break; Lev[u] =-1;            u = e[s[--top]].u; }        }    }returnRET;}voidDFS1 (intu) {Vis1[u] =1; for(inti = Head[u]; ~i; i = e[i].next) {intv = e[i].v;if(!vis1[v] && e[i].w) DFS1 (v); }}voidDFS2 (intu) {Vis2[u] =1; for(inti = Head[u]; ~i; i = e[i].next) {intv = e[i].v;if(!vis2[v] && e[i^1].W) DFS2 (v); }}intMain () {#ifdef j_sureFreopen ("000.in","R", stdin);//freopen ("999.out", "w", stdout);#endif     while(scanf("%d%d%d", &n, &m, &l), N) {intU, V, W;        Init (); src = n+m+1; Sink =0; for(inti =1; I <= N;        i++) {Add (src, I, INF); } for(inti =0; I < L; i++) {scanf("%d%d%d", &u, &v, &w); Line[i] = tot;//Store each network segment, eliminating the numbering processAdd (U, V, W);        } dinic (); DFS1 (SRC); DFS2 (sink);BOOLFIR =false; for(inti =0; I < L; i++) {if(!E[LINE[I]].W && vis1[e[line[i]].u] && VIS2[E[LINE[I]].V]) {if(FIR)printf(" "); FIR =true;printf("%d", i+1); }        }puts(""); }return 0;}
    1. If the capacity of the edge is only increased or decreased, the maximum flow will increase or decrease.
    2. Only a different color can guarantee that the increased traffic on that side will reach the meeting point from the source point.

"Maximum Flow | key Edge" ZOJ-1532 Internship

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.