1834: [Zjoi2010]network Network expansion time limit:3 Sec Memory limit:64 MB
submit:3735 solved:2001
[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 input 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. N<=1000,m<=5000,k<=10output
The output file contains two integers in a row, representing 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 1Sample Output13 19
HINT Source
Flood, the first question is the maximum flow, dinic run
Second, we build on the residual network, on those sides of the topic to the edge, traffic inf, cost for the topic
Dinic run out of the remaining side do not move, the cost is 0, s to 1 even a flow of k, the cost of 0 of the side run the minimum cost maximum flow can be
#include <bits/stdc++.h> #define LL long long#define inf 1e9+10using namespace std;inline int read () {int x=0;int f=1; Char Ch=getchar (); while (!isdigit (CH)) {if (ch== '-') F=-1;ch=getchar ();} while (IsDigit (ch)) {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} const int maxn=1e5+10;struct node{int y,next,back,flow,w;} E[maxn];int len,linkk[maxn],head,tail,level[1100],s,t,n,m,x[maxn],y[maxn],f[maxn],c[maxn],dis[1100],ans,vis[ 1100],q[maxn],k;inline void Insert (int x,int y,int f,int c) {E[++len].y=y;e[len].next=linkk[x];linkk[x]=len;e[len]. flow=f;e[len].back=len+1;e[len].w=c;e[++len].y=x;e[len].next=linkk[y];linkk[y]=len;e[len].flow=0;e[len].back= Len-1;e[len].w=-c;} namespace dinicc{inline bool Getlevel () {Head=tail=0;memset (level), level,-1,sizeof ( Head<tail) {int tn=q[++head];for (int i=linkk[tn];i;i=e[i].next) {if (Level[e[i].y]==-1&&e[i].flow) {level [E[i].y]=level[tn]+1;q[++tail]=e[i].y;}}} return level[t]>=0;} inline int getmaxflow (int x,int flow) {if (x==t) return flow;int f=0,d;for (int i=linkk[x];i;i=e[i].next) {if (e[i].flow&&level[e[i].y]==level[x]+1) {if (d= Getmaxflow (E[i].y,min (e[i].flow,flow-f)) {f+=d;e[i].flow-=d;e[e[i].back].flow+=d;if (f==flow) return F;}}} if (!f) Level[x]=-1;return F;} inline int dinic () {int ans=0,d;while (Getlevel ()) {while (D=getmaxflow (S,inf)) Ans+=d;} return ans;}} namespace zkww{inline bool Getdis () {memset (vis,0,sizeof (Vis)), memset (dis,10,sizeof (dis));d is[t]=0;deque<int >q;q.push_back (t), while (!q.empty ()) {int Tn=q.front (), Q.pop_front (), for (int i=linkk[tn];i;i=e[i].next) {if (dis[ Tn]-e[i].w<dis[e[i].y]&&e[e[i].back].flow) {dis[e[i].y]=dis[tn]-e[i].w;if (!vis[e[i].y]) {vis[e[i].y]= 1;if (!q.empty () &&dis[e[i].y]<dis[q.front ()) Q.push_front (E[I].Y); else Q.push_back (E[I].Y);}} vis[tn]=0;} return dis[s]<168430090;} inline int getcost (int x,int flow) {int f=0,d;vis[x]=1;if (x==t) return flow;for (int i=linkk[x];i;i=e[i].next) {if (e[ I].FLOW&&DIS[E[I].Y]==DIS[X]-E[I].W&&!VIS[E[I].Y]){if (D=getcost (E[i].y,min (Flow-f,e[i].flow))) {f+=d;e[i].flow-=d;e[e[i].back].flow+=d;ans+=e[i].w*d;if (F==flow) return f;}}} return F;} inline void zkw () {while (Getdis ()) {vis[t]=1;while (vis[t]) {memset (vis,0,sizeof (Vis)); Getcost (S,inf);}}} void Build () {S=0;t=n;insert (s,1,k,0); for (int i=1;i<=m;i++) {insert (X[i],y[i],inf,c[i]);}} int main () {using namespace Dinicc;using namespace Zkww;n=read (); M=read (); K=read (); s=1;t=n;for (int i=1;i<=m;i++) {x [I]=read (); Y[i]=read (); F[i]=read (); C[i]=read (); Insert (x[i],y[i],f[i],0);} Ans=dinic ();cout<<ans<< "; build (); ans=0;zkw (); Cout<<ans<<endl;return 0;}
Bzoj 1834 ZJOI2010 Network expansion