P3381 "template" Minimum cost maximum flow topic description
Title, a network diagram is given, along with its source and sink points, each edge is known for its maximum flow rate and unit traffic cost, and the minimum cost of its maximum flow and maximum flow is calculated.
Input/output format
Input format:
The first line contains four positive integers n, M, S, T, which represent the number of points, the number of forward edges, the number of source points, and the number of sink points.
The next M line contains four positive integer UI, VI, WI, FI, indicating that article I has an edge from the UI to reach VI, the edge is WI (that is, the edge of the maximum traffic is WI), the cost of unit traffic for FI.
Output format:
A row that contains two integers, followed by the maximum traffic and the minimum cost in the case of maximum traffic.
Input and Output Sample input example # #:
4 5 4 34 2 30 24 3 20 32 3 20 12 1 30 91 3 40 5
Sample # # of output:
50 280
Description
Time limit: 1000ms,128m
Data size:
For 30% data: n<=10,m<=10
For 70% data: n<=1000,m<=1000
For 100% data: n<=5000,m<=50000
Sample Description:
, the optimal scheme is as follows:
The first flow is 4-->3, the flow is 20 and the cost is 3*20=60.
The second stream is 4-->2-->3, the flow is 20, and the cost is (2+1) *20=60.
The third stream is 4-->2-->1-->3, the flow is 10 and the cost is (2+9+5) *10=160.
So the maximum flow is 50, in this case the minimum cost is 60+60+160=280.
So output 50 280.
90 Points of MCMF (Dijkstra is stuck constant)
#include <cstdio>#include<cstring>#include<queue>#definePir pair<int,int>#defineINF 0x33333333using namespacestd;Const intn=1e4+Ten;Const intm=1e5+Ten;structnode{intV,next,cap,cost; Node (intv=0,intnext=0,intcap=0,intcost=0): V (v), next (next), Cap (CAP), cost (cost) {}}e[m<<1];inttot=1;intN,m,s,t,head[n],pv[n],pe[n],dis[n],h[n];BOOLVis[n];voidAddintXintYintCapintCost ) {e[++tot]=node (y,head[x],cap,cost); HEAD[X]=tot;} Pir MCMF () {intflow=0, cost=0; while(1) {memset (DIS,0x33,sizeofdis); Priority_queue<pir,vector<pir>,greater<pir> >Q; Q.push (Make_pair (dis[s)=0, S)); while(!Q.empty ()) {PIR T=q.top (); Q.pop (); intx=T.second; if(T.first!=dis[x])Continue; if(x==t) Break; for(intI=head[x];i;i=E[i].next) { intv=e[i].v,newcost=e[i].cost+h[x]-H[v]; if(e[i].cap>0&&dis[v]>dis[x]+newcost) {Dis[v]=dis[x]+Newcost; Q.push (Make_pair (dis[v],v)); PV[V]=x;pe[v]=i; } } } if(Dis[t]==inf) Break; for(intI=1; i<=n;i++) H[i]=min (h[i]+Dis[i],inf); intnewflow=inf; for(intI=t;i!=s;i=Pv[i]) {Newflow=min (newflow,e[pe[i]].cap); } Flow+=Newflow; Cost+=newflow*H[t]; for(intI=t;i!=s;i=Pv[i]) {E[pe[i]].cap-=Newflow; E[pe[i]^1].cap+=Newflow; } } returnMake_pair (flow,cost);}intMain () {scanf ("%d%d%d%d",&n,&m,&s,&T); for(intI=1, x,y,z,w;i<=m;i++) scanf ("%d%d%d%d", &x,&y,&z,&w), add (X,y,z,w), add (Y,x,0,-V); Pir ans=MCMF (); printf ("%d%d", Ans.first,ans.second); return 0;}
100 points (changed to SPFA on the past)
/*take the cost as the weight value, find the minimum cost chain, and then on this chain to find a minimum flow, until the cost chain is not found. The minimum cost chain is also equivalent to the shortest path of seeking src->des. Use the Spfa+ek algorithm. Get MCMF algorithm*/#include<cstdio>#include<cstring>#include<queue>using namespacestd;Const intn=1e4+Ten;Const intm=1e5+Ten;structnode{intV,next,cap,flow,cost; Node (intv=0,intnext=0,intcap=0,intflow=0,intcost=0): V (v), next (next), Cap (CAP), flow, cost (cost) {}}e[m<<1];inttot=1;intN,m,s,t,head[n],p[n],dis[n],num[n];BOOLVis[n];voidAddintXintYintCapintCost ) {e[++tot]=node (Y,head[x],cap,0, cost); HEAD[X]=tot;}BOOLSPFA (int&flow,int&Cost ) {memset (Vis,0,sizeofvis); memset (DIS,0x3f3f3f3f,sizeofdis); Queue<int>Q; Q.push (S); Vis[s]=1; Dis[s]=0; Num[s]=0x7fffffff; P[s]=0; while(!Q.empty ()) { intx=Q.front (); Q.pop (); VIS[X]=0; for(intI=head[x];i;i=E[i].next) { intv=e[i].v; if(dis[v]>dis[x]+e[i].cost&&e[i].cap>E[i].flow) {Dis[v]=dis[x]+E[i].cost; P[V]=i; NUM[V]=min (num[x],e[i].cap-E[i].flow); if(!Vis[v]) {Vis[v]=1; Q.push (v); } } } } if(dis[t]==0x3f3f3f3f)return 0; Flow+=Num[t]; Cost+=num[t]*Dis[t]; for(inti=t;i!=s;i=e[p[i]^1].v) {E[p[i]].flow+=Num[t]; E[p[i]^1].flow-=Num[t]; } return 1;}intMain () {scanf ("%d%d%d%d",&n,&m,&s,&T); for(intI=1, x,y,z,w;i<=m;i++) scanf ("%d%d%d%d", &x,&y,&z,&w), add (X,y,z,w), add (Y,x,0,-W); intflow=0, cost=0; while(SPFA (flow,cost)); printf ("%d%d\n", Flow,cost); return 0;}
P3381 "template" Minimum cost maximum flow