1834: [Zjoi2010]network Network expansion time limit:3 Sec Memory limit:64 MB
submit:2677 solved:1359
[Submit] [Status] [Discuss] 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. The first line of the input file contains three integer n,m,k, indicating the number of points, sides, and the amount of traffic required to the graph. The next M-line contains four integer u,v,c,w, representing an edge from U to V with a capacity of C and a expansion fee of W. The output file row contains two integers that represent the answers to question 1 and question 2, respectively. Sample Input5 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 Output13 19
30% of the data, n<=100
100% of the data, n<=1000,m<=5000,k<=10
1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineN 1023335#include <queue>6 #defineINF 21474836477 using namespacestd;8InlineintRead () {9 intx=0;CharCh=GetChar ();Ten while(ch<'0'|| Ch>'9') ch=GetChar (); One while(ch>='0'&&ch<='9') x=x*Ten+ch- -, ch=GetChar (); A returnx; - } - structnode{ the intW,to,next,c, from; -}e[n<<1],e[n<<1]; - intHead[n],dis[n], from[n],i,j,zs1,zs2,zs3,zs4,tot=1, T,anss,m,n,sum,flow; - BOOLVis[n]; + inttot1,tot2,num[n][2]; -InlinevoidInsintUintVintWintCost ) { +E[++tot].to=v; E[tot].next=head[u]; Head[u]=tot; E[tot].w=w; E[tot].c=cost; E[tot]. from=u; A } atInlineBOOLSPFA () { - for(intI=0; i<=t;i++) Dis[i]=inf; vis[0]=1; dis[0]=0; queue<int>q; Q.push (0); - while(!Q.empty ()) { - intx=Q.front (); Q.pop (); - for(intk=head[x];k;k=e[k].next) - if(Dis[x]+e[k].c<dis[e[k].to] && e[k].w>0) { in from[e[k].to]=K; -dis[e[k].to]=dis[x]+e[k].c; to if(!Vis[e[k].to]) { +vis[e[k].to]=1; Q.push (e[k].to); - } the } *vis[x]=0; $ }Panax Notoginseng if(Dis[t]==inf)return 0;Else return 1; - } theInlineBOOLBFs () { + for(i=0; i<=t;i++) dis[i]=-1; queue<int>q; Q.push (1); dis[1]=0; A while(!Q.empty ()) { the intx=Q.front (); Q.pop (); + for(intk=head[x];k;k=e[k].next) - if(dis[e[k].to]<0&& e[k].w>0) { $dis[e[k].to]=dis[x]+1; Q.push (e[k].to); $ } - } - if(dis[t]>0)return 1;Else return 0; the } - intFindintXintLow ) {Wuyi if(x==t)returnLow ; the intDelta=Low,now; - for(intk=head[x];k;k=e[k].next) Wu if(e[k].w>0&& dis[e[k].to]==dis[x]+1){ -now=Find (E[k].to,min (E[k].w,delta)); AboutE[k].w-=now; e[k^1].w-=now; delta-=Now ; $ if(!delta)returnLow ; - } -dis[x]=-1; - returnlow-Delta; A } +Inlinevoidrun () { the intx=inf; - for(intI= from[t];i;i= from[E[i]. from]) x=min (X,E[I].W); $ for(intI= from[t];i;i= from[E[i]. from]) { theAnss+=e[i].c*x;e[i].w-=x; e[i^1].w+=x; the } the } the intMain () { -N=read ();intRoad=read (); M=read (); t=N; in for(i=1; i<=road;i++) { theZs1=read (), Zs2=read (), Zs3=read (), zs4=read (); theIns (ZS1,ZS2,ZS3,0); Ins (ZS2,ZS1,0,0); num[++tot1][0]=tot-1, num[tot1][1]=ZS3; AboutE[++tot2]. from=ZS1; E[TOT2].TO=ZS2; E[tot2].c=Zs4; the } the while(BFS ()) { the while(Sum=find (1, INF)) flow+=sum; + } -printf"%d", flow); the for(i=1; i<=tot1;i++) {Bayie[num[i][0]].w=num[i][1]; e[num[i][0]^1].w=0; the } the intU,v,cost; -Ins0,1, Flow+m,0), INS (1,0,0,0); - for(i=1; i<=tot2;i++) { theU=e[i]. from, v=e[i].to,cost=e[i].c; theIns (u,v,inf,cost); Ins (V,u,0,-Cost ); the } the while(SPFA ()) run (); -printf"%d", ANSS); the}
View Code
bzoj1834: [Zjoi2010]network Network expansion