Codeforces 270E flawed Flow network flow problem

Source: Internet
Author: User

Test instructions: Give some edges, give the capacity of the edge. Lets you determine One direction for all sides to make the maximum flow.

The problem is not to seek the maximum flow, but to seek the flow of each side, the problem is to examine the Basic Law of network flow.

If a graph has the largest, then there is bound to the source point is flowing, the edge of the meeting point is bound to flow, all the other points into and out of the flow is equal.

We can solve it according to this law.

The total flow of all points (except source and sink) is calculated first, which represents twice times the flow of incoming traffic, and each time it flows through that edge, it subtracts twice times the amount of incoming traffic when it is updated.

From the source point of wide search each point, the search process can determine the flow through the edge, when a point of the remaining total traffic is 0 o'clock, indicating that the flow into the point of the traffic has been processed, put this into the stack.

Special Note: When this point is a meeting point when not into the stack, or will return from the meeting point, does not conform to the Basic Law.

#include <bits/stdc++.h>using namespacestd;Const intMAXN =200010;Const intMAXM = MAXN *2;structEdge {intu,v,w; intNext; intType,id;} EDGE[MAXM];intHead[maxn],tot;voidInit () {tot =0; memset (head,-1,sizeof(head));}voidAdd_edge (intUintVintWintTypeintID) {edge[tot].u=u; EDGE[TOT].V=v; EDGE[TOT].W=W; Edge[tot].type=type; Edge[tot].id=ID; Edge[tot].next=Head[u]; Head[u]= tot++;} Queue<int>Q;intn,m;BOOLVIS[MAXN];intANS[MAXN],VAL[MAXN];intMain () { while(SCANF ("%d%d", &n,&m)! =EOF)        {init (); Memset (Val,0,sizeof(Val));  for(inti =0; i < M; i++) {            intu,v,w; scanf ("%d%d%d",&u,&v,&W); Add_edge (U,v,w,0, i +1); Add_edge (V,u,w,1, i +1); Val[u]+=W; VAL[V]+=W; }         while(!q.empty ()) Q.pop (); vis[1] =true; Q.push (1);  while(!Q.empty ()) {            intU =Q.front (); Q.pop ();  for(inti = Head[u]; I! =-1; i =Edge[i].next) {                intv =edge[i].v; if(Vis[v])Continue; VAL[V]-=2*EDGE[I].W; Ans[edge[i].id]=Edge[i].type; if(Val[v] = =0&& V! =N) {Vis[v]=true;                Q.push (v); }            }        }         for(inti =1; I <= M; i++) printf ("%d\n", Ans[i]); }    return 0;}

Codeforces 270E flawed Flow network flow problem

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.