[Bzoj 1834] [Zjoi2010]network Network expansion (fee flow)

Source: Internet
Author: User

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. 1, in the case of no expansion, the maximum flow of 1 to N, 2, the maximum flow of 1 to n increases the minimum cost of expansion required by K.

Solution

Find out the maximum flow Maxflow first

For the minimum cost of expansion, for each edge, build a capacity of C cost of 0 of the edge, and build a capacity for the INF cost of the edge of W

The cost of running the flow of money into the Maxflow+k

#include <iostream>#include<cstdio>#include<queue>#include<cstdlib>#include<cstring>#defineINF 0x3f3f3f3fusing namespacestd;ints,t,n,m,k,head[1005],cnt=0, a[1005],dis[1005],pre[1005];intFlow,cost;BOOLinq[1005];intRead () {intx=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9'){        if(c=='-') f=-1; c=GetChar (); }     while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar (); }    returnx*F;}structnode{intNext from, To,cap,w;} edges[20005];voidAddedge (intUintVintCintW) {Edges[cnt].next=Head[u]; Head[u]=CNT; EDGES[CNT]. from=u; Edges[cnt].to=v; Edges[cnt].cap=C; EDGES[CNT].W=W; CNT++;}voidInsertintUintVintCintW)    {Addedge (u,v,c,w); Addedge (V,u,0,-W);} Queue<int>Q;voidMCMF (intx) {Flow=0, cost=0;  while(1) {memset (A,0,sizeof(a)); memset (DIS,0x3f,sizeof(DIS));        Q.push (s); Pre[s]=-1;d is[s]=0, a[s]=x?x-flow:inf,inq[s]=1;  while(!Q.empty ()) {            intu=Q.front (); Q.pop (), Inq[u]=0;  for(intI=head[u];~i;i=Edges[i].next) {                intv=edges[i].to; if(!x&&edges[i].cap==inf)Continue; if(dis[v]>dis[u]+edges[i].w&&edges[i].cap>0) {Dis[v]=dis[u]+EDGES[I].W; A[V]=min (a[u],edges[i].cap); PRE[V]=i; if(!inq[v]) {Q.push (v); inq[v]=1;} }            }        }        if(a[t]==0) Break; Flow+=A[t]; intp=T; Cost+=a[t]*Dis[t];  while(pre[p]!=-1) {Edges[pre[p]].cap-=A[t]; EDGES[PRE[P]^1].cap+=A[t]; P=EDGES[PRE[P]]. from; }    }}intMain () {memset (head,-1,sizeof(head)); N=read (), M=read (), k=read (); S=1, t=N;  for(intI=1; i<=m;i++)    {        intU=read (), V=read (), C=read (), w=read (); Insert (U,v,c,0);    Insert (U,V,INF,W); } MCMF (0); printf ("%d", Flow);    MCMF (k); printf ("%d\n", cost); return 0;}

[Bzoj 1834][zjoi2010]network Network expansion (charge flow)

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.