BZOJ1834: [Zjoi2010]network Network expansion

Source: Internet
Author: User

"Portal: BZOJ1834" Brief Test Instructions:

Given n points, M edge of the graph, give each side of the flow C and cost W (each edge can be amplified their own traffic, each increase of 1 traffic will require the cost of W)

The maximum flow from 1 to n is calculated, and the minimum cost of the maximum flow +k is obtained.

The following:

The first answer is to use the network flow directly to be sure is no problem

The second answer needs to be done with a fee stream.

First, because the first answer to the time has been able to form the maximum flow of the path on each side of the traffic is reduced, so left a residual map

We will find that in fact, there are still some side of the flow of residual diagram is not shed, then we use the remaining traffic on each side to build a traffic for the current remaining traffic and the cost of 0 of the edge, and build an unlimited flow, the cost of the original cost of the side, if this side has been shed, then we directly build an unlimited flow, , and the two steps can be combined in the actual operation

In fact, we do not need to re-build all the edges, only need to add an unlimited flow of each side, the cost of the original cost of the edge, and the remaining traffic is built on the edge of the residual map

Finally we have to create a new meeting point, so that N to the meeting point, the flow is K, the cost is 0, so that the total flow can not exceed the K

Reference Code:
#include <cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;structnode{intX,y,c,d,next,other; Node () {D=0; }}a[21000];intlen,last[5100];voidInsintXintYintCintd) {    intk1=++len,k2=++Len; a[k1].x=x;a[k1].y=y;a[k1].c=c;a[k1].d=D; A[k1].next=last[x];last[x]=K1; a[k2].x=y;a[k2].y=x;a[k2].c=0; a[k2].d=-D; A[k2].next=last[y];last[y]=K2; A[k1].other=K2; A[k2].other=K1;}intst,ed;inth[5100],list[5100];BOOLBt_h () {intHead=1, tail=2; memset (H,0,sizeof(h)); H[ST]=1; list[1]=St;  while(head!=tail) {        intx=List[head];  for(intk=last[x];k;k=A[k].next) {            inty=a[k].y; if(h[y]==0&&a[k].c>0) {H[y]=h[x]+1; List[tail++]=y; }} head++; }    if(h[ed]==0)return false; Else return true;}intFindflow (intXintf) {    if(x==ed)returnF; ints,t=0;  for(intk=last[x];k;k=A[k].next) {        inty=a[k].y; if(h[y]== (h[x]+1) &&a[k].c>0&&f>t) {s=findflow (Y,min (a[k].c,f-t)); T+=s; A[K].C-=s;a[a[k].other].c+=s; }    }    if(t==0) h[x]=0; returnt;}intd[5100];BOOLv[5100];intans;intpos[5100],pre[5100];BOOLSPFA () { for(inti=st;i<=ed;i++) d[i]=999999999; D[ST]=0; memset (V,false,sizeof(v)); V[ST]=true; intHead=1, tail=2; list[1]=St;  while(head!=tail) {        intx=List[head];  for(intk=last[x];k;k=A[k].next) {            inty=a[k].y; if(a[k].c>0&&d[y]>d[x]+a[k].d) {D[y]=d[x]+A[K].D; Pos[y]=x; Pre[y]=K; if(v[y]==false) {V[y]=true; List[tail++]=y; }}} head++; V[X]=false; }    if(d[ed]==999999999)return false; Else return true;}voidFlow () { while(SPFA ()) {ans+=d[ed];  for(intx=ed;x!=st;x=Pos[x]) {A[PRE[X]].C--; A[A[PRE[X]].OTHER].C++; }    }}intx[5100],y[5100],c[5100],w[5100];intMain () {intn,m,k; scanf ("%d%d%d",&n,&m,&k); Len=0; Memset (Last,0,sizeof(last)); St=1; ed=N;  for(intI=1; i<=m;i++) {scanf ("%d%d%d%d",&x[i],&y[i],&c[i],&W[i]); Ins (X[i],y[i],c[i],0); } ans=0;  while(Bt_h () = =true) {ans+=findflow (St,999999999); } printf ("%d", ans);  for(intI=1; i<=m;i++) ins (x[i],y[i],k,w[i]); Ed=n+1; INS (N,n+1K0); Ans=0;    Flow (); printf ("%d\n", ans); return 0;}

BZOJ1834: [Zjoi2010]network 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.