The first question directly runs the maximum flow can. The plan is built according to the cost flow, the cost is 0.
For the second question, in the first question Dinic the remainder of the network to build, the original image of each side (I,J), built (I,J,INF,CIJ), that the cost of C can be augmented by the road. Then from the new source point, the s,1,k,0 represents the traffic to increase the K. Run the minimum cost maximum flow.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespaceStdConst intn=1000005, inf=1e9;intN,m,k,h[n],cnt=1, Le[n],s,t,ans,dis[n],fr[n],c[n];BOOLV[n];structqwe{intNe,no,to,va,c;} E[n];intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ') {if(p=='-') f=-1; P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48; P=getchar (); }returnR*f;}voidAddintUintVintWintc) {cnt++; E[cnt].ne=h[u]; E[cnt].no=u; E[cnt].to=v; E[cnt].va=w; E[cnt].c=c; h[u]=cnt;}voidInsintUintVintWintc) {Add (u,v,w,c); Add (V,u,0,-c);}BOOLBFS () {queue<int>q; Memset (LE,0,sizeof(LE)); le[s]=1; Q.push (s); while(!q.empty ()) {intU=q.front (); Q.pop (); for(intI=h[u];i;i=e[i].ne)if(e[i].va>0&&!le[e[i].to]) {Le[e[i].to]=le[u]+1; Q.push (e[i].to); } }returnLE[T];}intDfsintUintf) {if(!f| | U==T)returnFintus=0; for(intI=h[u];i&&us<f;i=e[i].ne)if(e[i].va>0&&le[e[i].to]==le[u]+1) {intT=dfs (E[i].to,min (e[i].va,f-us)); e[i].va-=t; e[i^1].va+=t; us+=t; }if(!us) le[u]=0;returnUS;}intDinic () {intRe=0; while(BFS ())) Re+=dfs (S,inf);returnRe;}BOOLSPFA () {queue<int>q; for(inti=s;i<=t;i++) Dis[i]=inf; dis[s]=0; v[s]=0; Q.push (s); while(!q.empty ()) {intU=q.front (); Q.pop (); v[u]=0; for(intI=h[u];i;i=e[i].ne)if(e[i].va>0&&DIS[E[I].TO]>DIS[U]+E[I].C) {dis[e[i].to]=dis[u]+e[i].c; Fr[e[i].to]=i;if(!v[e[i].to]) {v[e[i].to]=1; Q.push (e[i].to); } } }returnDis[t]!=inf;}voidMCF () {intX=inf; for(intI=fr[t];i;i=fr[e[i].no]) x=min (X,e[i].va); for(intI=fr[t];i;i=fr[e[i].no]) {e[i].va-=x; e[i^1].va+=x; Ans+=e[i].c*x; }}intMain () {N=read (), M=read (), K=read (); s=1, T=n; for(intI=1; i<=m;i++) {intX=read (), Y=read (), Z=read (); C[i]=read (); Ins (x, Y, Z,0); } printf ("%d ", Dinic ()); s=0, T=n;intnow=cnt; for(intI=2; i<=now;i+=2) Ins (e[i].no,e[i].to,inf,c[i>>1]); INS (S,1K0); while(SPFA ()) MCF (); printf"%d\n", ans);return 0;}
Bzoj 1834: [Zjoi2010]network Network Expansion "maximum flow + minimum cost maximum flow"