C + + Path advanced--Network flow (network expansion)

Source: Internet
Author: User

1362 Network Expansion

Provincial Team Tryouts

time limit: 2 sspace limit: 128000 KBtopic rank: Master MasterTitle Description Description

Given a forward graph, each edge has a capacity of C and an expansion fee of W. The expansion fee here refers to the cost of expanding the capacity by 1. Please:

1, in the case of no expansion, the maximum flow of 1 to N ;

2, the maximum flow of 1 to N to increase the minimum required for the expansion of K cost.

Enter a description Input Description

The first line of the input file contains three integers N,M,K, indicating the number of points, sides, and the amount of traffic that is required for the graph.

The next M -line contains four integers per line u,v,c,W, representing a from u to v, a capacity of C, The expansion cost is the side of W .

Output description Output Description

The output file contains two integers in a row, representing the answers to question 1 and question 2, respectively.

Sample input Sample Input

5 8 2

1 2 5 8

2 5 9 9

5 1 6 2

5 1 1 8

1 2 8 7

2 5 4 9

1 2 1 1

1 4 2 1

Sample output Sample Output

13 19

Data range and Tips Data Size & Hint

30% of the data,N<=100

100% of the data,N<=1000,M<=5000,K<=10

Exercises

Add new Edge running minimum cost maximum flow on participating networks with maximum flow!

Code:

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <queue>5 #defineMAXN 100106 #defineINF 0x7fffffff7 #defineS 18 #defineT N9   Ten using namespacestd; One    A intN,m,k,cnt=1, VW[MAXN],ANS1,ANS2,U[MAXN],V[MAXN],HEAD[MAXN],X,DIS[MAXN],INQ[MAXN], from[MAXN]; -    - structSS the    { -      intto ; -      intNext; -      intC; +      intW; -      int  from; +}e[100010]; A       at voidAddintUintVintCintW) -    { -e[++cnt].to=v; -e[cnt].next=Head[u]; -E[CNT]. from=u; -E[cnt].c=C; ine[cnt].w=W; -head[u]=CNT; to    } +    - voidInsertintUintVintCintW) the    { * Add (u,v,c,w); $Add (V,u,0,-W);Panax Notoginseng    } -    the BOOLBFS () +    { A       for(intI=1; i<=t;i++) dis[i]=inf; thequeue<int>que; + Que.push (S); -dis[s]=0; $       while(!que.empty ()) $         { -            intnow=Que.front (); - Que.pop ();  the             for(intI=head[now];i;i=e[i].next) -              if(e[i].c&&dis[e[i].to]>dis[now]+1)Wuyi                   { thedis[e[i].to]=dis[now]+1; - Que.push (e[i].to); Wu                      if(e[i].to==t)return 1;  -                      } About            } $     return 0;  -    } -       - intDinic (intXintINF) A    { +      if(x==t)returnINF; the      intrest=INF; -       for(intI=head[x];i;i=e[i].next) $        if(e[i].c&&dis[e[i].to]==dis[x]+1) the           { the             intnow=dinic (E[i].to,min (REST,E[I].C)); thee[i].c-=Now ; the             if(!now) dis[now]=0; -e[i^1].c+=Now ; inrest-=Now ; the           } the     returninf-rest;  About    } the       the BOOLSPFA () the    { +         for(intI=0; i<=t;i++) dis[i]=inf; -queue<int>que; theQue.push (0);Bayiinq[0]=1; thedis[0]=0; the         while(!que.empty ()) -           { -              intnow=Que.front (); the Que.pop (); theinq[now]=0; the               for(intI=head[now];i;i=e[i].next) the                 if(e[i].c&&dis[e[i].to]>dis[now]+E[I].W) -                     {  thedis[e[i].to]=dis[now]+E[I].W; the                           from[e[i].to]=i; the                          if(!inq[e[i].to])94                            { theinq[e[i].to]=1; the Que.push (e[i].to);  the                              }98                      } About           } -         if(Dis[t]==inf)return 0;101                    Else return 1; 102       }   103    104 intBCF () the    {106      intx=inf;107       for(intI= from[t];i;i= from[E[i]. from])108x=min (x,e[i].c);109       for(intI= from[t];i;i= from[E[i]. from]) the         {111ans2+=x*E[I].W; thee[i].c-=x;113e[i^1].c+=x; the             }     the        }     the          117 intMain ()118    {119scanf"%d%d%d",&n,&m,&K); -       for(intI=1; i<=m;i++)121        {122           intu,v,w,c;123scanf"%d%d%d%d",&u[i],&v[i],&c,&vw[i]);124Insert (U[i],v[i],c,0); the           }126       while(BFS ())127          while(X=dinic (S,inf)) ans1+=x; -Insert0,1K0); 129printf"%d", ans1);  the       for(intI=1; i<=m;i++)131 Insert (U[i],v[i],k,vw[i]); the       while(SPFA ()) BCF ();133printf"%d\n", ANS2); 134      return 0;135}

C + + Path advanced--Network flow (network expansion)

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.