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
first question maximum flowsecond questionset up super source and super sink, super Source to 1 connecting edge, capacity of k,n to super sink, capacity ibid. Each side of the original set up another, the capacity of the INF, the cost of W, in the original image augmentation, run the minimum cost maximum flowbut!!! The new side must be completed after the first question to build ... slightly larger code size ... But I'm all over the board ...
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <iostream>5 using namespacestd;6 Const intn=55555, inf=1000000;7 structee{intTo,next,f,w;} e[n*2];8 intS,t,cnt=1, N,k,ans1,ans2,timer,m,u[n],v[n],w[n],c[n],mid;9 intHead[n],dis[n],pre[n],q[n];Ten BOOLinq[n],flag=1; One voidInsintUintVintFintW) { Ae[++cnt].to=v,e[cnt].next=head[u],e[cnt].f=f,e[cnt].w=w,head[u]=CNT; -e[++cnt].to=u,e[cnt].next=head[v],e[cnt].f=0, e[cnt].w=-w,head[v]=CNT; - } the BOOLSPFA () { - for(intI=1; i<=t;i++) dis[i]=inf; - intH=0, t=1; -q[t]=s;dis[s]=0; inq[s]=1; + while(h!=t) { - intNOW=Q[++H];if(h==2501) h=0; + for(intI=head[now];i;i=E[i].next) { A intv=e[i].to; at if(dis[v]>dis[now]+e[i].w&&e[i].f) { -dis[v]=dis[now]+E[I].W; -pre[v]=i; - if(!Inq[v]) { -Q[++t]=v;if(t==2501) t=0; -inq[v]=1; in } - } to } +inq[now]=0; - } the if(Dis[t]==inf)return 0; * return 1; $ }Panax Notoginseng - voidUpdata () { the inttmp=t,flow=inf; + while(tmp!=S) { A intl=pre[tmp],v=e[l].to; theflow=min (flow,e[l].f); +tmp=e[l^1].to; - } $tmp=T; $ while(tmp!=S) { - intl=pre[tmp],v=e[l].to; -e[l].f-=flow;e[l^1].f+=flow; thetmp=e[l^1].to; - }Wuyimid+=flow; the if(mid>=k) flag=0; -ans2+=dis[t]*flow; Wu } - About BOOLBFs () { $ for(intI=1; i<=t;i++) dis[i]=inf; - intH=0, t=1, now; -q[1]=1;d is[1]=0; - while(h!=t) { Anow=q[++h]; + for(intI=head[now];i;i=E[i].next) { the intv=e[i].to; - if(e[i].f&&dis[now]+1<Dis[v]) { $dis[v]=dis[now]+1; the if(v==n)return 1; theq[++t]=v; the } the } - } in if(Dis[n]==inf)return 0;return 1; the } the About intDinic (intNowintf) { the if(now==n)returnF; the intrest=F; the for(intI=head[now];i;i=E[i].next) { + intv=e[i].to; - if(e[i].f&&dis[v]==dis[now]+1&&rest) { the intt=dinic (V,min (REST,E[I].F));Bayi if(!t) dis[v]=0; thee[i].f-=T; thee[i^1].f+=T; -rest-=T; - //if (t) printf ("%d%d%d\n", now,v,e[i].f); the } the } the returnF-rest; the } - the intMain () { thescanf"%d%d%d",&n,&m,&k); thes=0, t=n+1;94 for(intI=1; i<=m;i++){ thescanf"%d%d%d%d",&u[i],&v[i],&c[i],&w[i]); theIns (U[i],v[i],c[i],0); the }98Ins (S,1K0); Ins (N,t,k,0); About while(BFS ()) -Ans1+=dinic (1, INF);101 for(intI=1; i<=m;i++) ins (u[i],v[i],inf,w[i]);102 while(flag&&SPFA ())103 Updata ();104printf"%d%d", ans1,ans2); the}
"Bzoj 1834" [Zjoi2010]network Network expansion