Bzoj
Luogu
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.
Input
The first line of the input file contains three integer n,m,k, which represents 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.
Output
The output file contains two integers in a row, representing the answers to question 1 and question 2, respectively.
Sample Input
5 8 Span class= "DV" >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 Output
1319
30% of the data, n<=100
100% of the data, n<=1000,m<=5000,k<=10
Exercises
First asked about the bare dinic
The second question joins the capacity of the K-fee for the side of W, which can run on the original image, because it is not affected.
Code
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespaceStdConst intN =5005;Const intINF =1e9;structedge{intTo,next,w,cost;} a[n<<2];intN,m,k,s,t,u[n],v[n],c[n],w[n],head[n],cnt=1,dep[n],cur[n],dis[n],vis[n],pe[n],ans;queue<int>Q;intGI () {intx=0, w=1;CharCh=getchar (); while((ch<' 0 '|| Ch>' 9 ') &&ch!='-') Ch=getchar ();if(ch=='-') w=0, Ch=getchar (); while(ch>=' 0 '&&ch<=' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar ();returnW?x:-x;}voidLinkintUintVintWintCost) {a[++cnt]= (edge) {v,head[u],w,cost}; head[u]=cnt; A[++cnt]= (Edge) {U,head[v],0,-cost}; head[v]=cnt;}BOOLBFS () {memset (DEP,0,sizeof(DEP)); dep[1]=1; Q.push (1); while(! Q.empty ()) {intU=q.front (); Q.pop (); for(intE=head[u];e;e=a[e].next)if(A[e].w&&!dep[a[e].to]) Dep[a[e].to]=dep[u]+1, Q.push (a[e].to); }returnDep[n];}intDfsintUintFlow) {if(u==n)returnFlow for(int&e=cur[u];e;e=a[e].next)if(A[e].w&&dep[a[e].to]==dep[u]+1) {intTemp=dfs (A[e].to,min (FLOW,A[E].W));if(temp) {a[e].w-=temp;a[e^1].w+=temp;returntemp;} }return 0;}intDinic () {intres=0; while(BFS ()) { for(intI=1; i<=n;i++) Cur[i]=head[i]; while(intTemp=dfs (1, INF)) Res+=temp; }returnRes;}BOOLSPFA () {memset (DIS, the,sizeof(dis)); Dis[n+1]=0; Q.push (n+1); while(! Q.empty ()) {intU=q.front (); Q.pop (); for(intE=head[u];e;e=a[e].next) {intv=a[e].to;if(A[e].w&&dis[v]>dis[u]+a[e].cost) {dis[v]=dis[u]+a[e].cost;pe[v]=e;if(!vis[v]) vis[v]=1, Q.push (v); }} vis[u]=0; }if(dis[n]==dis[0])return false;intSum=inf; for(intI=n;i!=n+1; i=a[pe[i]^1].to) sum=min (SUM,A[PE[I]].W); for(intI=n;i!=n+1; i=a[pe[i]^1].to) a[pe[i]].w-=sum,a[pe[i]^1].w+=sum; Ans+=dis[n]*sum;return true;}intMain () {N=gi (); M=gi (); K=gi (); for(intI=1; i<=m;i++) {U[i]=gi (); V[i]=gi (); C[i]=gi (); W[i]=gi (); Link (U[i],v[i],c[i],0); } Link (n+1,1K0); printf"%d ", Dinic ()); for(intI=1; i<=m;i++) Link (u[i],v[i],k,w[i]); while(SPFA ()); printf"%d\n", ans);return 0;}
[BZOJ1834] [Zjoi2010]network Network expansion