Codeforces 362E Petya and Pipes cost flow diagram

Source: Internet
Author: User

Test instructions

Add capacity to some sides of a network, increasing the sum of the maximum to K, making the maximum flow maximum.

Cost flow: The cost of increasing unit traffic on an edge.

Then it is possible to build 2 edges between 2 points, and the first given edge (u,v,x,0) has an edge cost of 0

At the same time the other side (u,v,k,1) costs 1, you can find the maximum flow by limiting the corresponding cost at the time of augmentation

Individuals feel that the reason for this is that each time the credit is optimal. Therefore, the optimal solution can be obtained by limiting the final cost not exceeding K

#include <map>#include<Set>#include<list>#include<cmath>#include<ctime>#include<deque>#include<stack>#include<queue>#include<cctype>#include<cstdio>#include<string>#include<vector>#include<climits>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#defineLL Long Long#definePI 3.1415926535897932626using namespacestd;intgcdintAintb) {returnA% b = =0? B:GCD (b, a%b);}Const intMAXN = the;Const intINF =0x3f3f3f3f;structnode{intU,v,next; intFlow,cap,cost;} EDGE[MAXN* MAXN *4];intCnt,src,tag;intc,f;intK,n;queue<int>Q;BOOLINQ[MAXN];intD[MAXN];intHEAD[MAXN],P[MAXN];inttot =0;voidinit () {memset (head,-1,sizeof(head)); Tot=0;}voidAdd_edge (intUintVintCapintCost ) {edge[cnt].u=u; EDGE[CNT].V=v; Edge[cnt].cap=cap; Edge[cnt].flow=0; Edge[cnt].cost=Cost ; Edge[cnt].next=Head[u]; Head[u]= cnt++; //ReverseEDGE[CNT].V =u; EDGE[CNT].U=v; Edge[cnt].flow=0; Edge[cnt].cap=0; Edge[cnt].cost= -Cost ; Edge[cnt].next=Head[v]; HEAD[V]= cnt++;}BOOLSPFA (intSintt) {     while(!q.empty ()) Q.pop (); memset (INQ,false,sizeof(INQ)); memset (d,0x3f,sizeof(d)); Memset (P,-1,sizeof(p)); D[s]=0;    Q.push (s); Inq[s]=true;  while(!Q.empty ()) {        intU =Q.front (); Q.pop (); Inq[u]=false;  for(inti = Head[u]; I! =-1; i =Edge[i].next) {            intv =edge[i].v; if(D[v] > D[u] + edge[i].cost && edge[i].cap >Edge[i].flow) {D[v]= D[u] +Edge[i].cost; P[V]=i; if(!Inq[v])                    {Q.push (v); INQ[V]=true; }            }        }    }    if(D[tag] = = INF)return false; intA =INF;  for(inti = P[tag]; I! =-1; i =p[edge[i].u]) a= Min (A,edge[i].cap-Edge[i].flow); if(C + d[tag] * a >K) {F+ = (k-c)/D[tag]; return false; }    return true;}voidSlove () {C= F =0;  while(SPFA (Src,tag)) {intA =INF;  for(inti = P[tag]; I! =-1; i =p[edge[i].u]) a= Min (A,edge[i].cap-Edge[i].flow);  for(inti = P[tag]; I! =-1; i =P[EDGE[I].U]) {Edge[i].flow+=A; Edge[i^1].flow-=A; } C+ = D[tag] *A; F+=A; }}intMain () { while(SCANF ("%d%d", &n,&k)! =EOF)        {init ();  for(inti =1; I <= N; i++)             for(intj =1; J <= N; J + +)        {            intx; scanf ("%d",&x); if(x) {Add_edge (i,j,x,0); Add_edge (I,j,k,1); }} src=1; Tag=N;        Slove (); printf ("%d\n", F); }    return 0;}

Codeforces 362E Petya and Pipes cost flow diagram

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.