Title 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 sample #: Copy
4 5 4 34 2 30 24 3 20 32 3 20 12 1 30 91 3 40 5
Output Example # #: Replication
50 280
Description
Time limit: 1000ms,128m
(BYX: The last two points changed to 1200ms)
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.
Dijstra fee Flow It's really not the usual fast
Direct amputated SPFA
There's a good blog to write about.
http://www.yhzq-blog.cc/%E6%9C%80%E5%B0%8F%E8%B4%B9%E7%94%A8%E6%9C%80%E5%A4%A7%E6%B5%81%E7%AE%97%E6%B3%95%E6% 80%bb%e7%bb%93/
The other is the last word why *h, not *dis
I personally understand that because of the existence of h at the time of the shortest circuit, the dis is not actually dis, and h is the actual dis.
//Luogu-judger-enable-o2#include <cstdio>#include<cstring>#include<queue>#definePair pair<int,int>#defineFi first#defineSe Second#defineAddedge (x,y,f,z) Add_edge (x,y,f,z); Add_edge (y,x,0,-z);#defineGetChar () (p1==p2&& (p2= (p1=buf) +fread (Buf,1,maxn,stdin), P1==P2)? eof:*p1++)Charbuf[1<< -],*p1=buf,*p2=buf;using namespacestd;Const intmaxn=1e6+1, inf=1e8+Ten; inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,s,t;structnode{intU,V,F,W,NXT;} EDGE[MAXN];inthead[maxn],num=2; inlinevoidAdd_edge (intXintYintFintz) {edge[num].u=x; EDGE[NUM].V=y; EDGE[NUM].F=F; EDGE[NUM].W=Z; EDGE[NUM].NXT=Head[x]; HEAD[X]=num++;}intH[MAXN],DIS[MAXN],PREPOINT[MAXN],PREEDGE[MAXN]; Pair Dij () {intansflow=0, anscost=0; while(1) {priority_queue<Pair>Q; memset (DIS,0xf,sizeof(DIS)); Dis[s]=0; Q.push (Make_pair (0, S)); while(Q.size ()! =0) {Pair P=q.top (); Q.pop (); if(-p.fi!=dis[p.se])Continue; if(p.se==t) Break; for(inti=head[p.se];i!=-1; i=edge[i].nxt) { intnowcost=edge[i].w+h[p.se]-H[EDGE[I].V]; if(edge[i].f>0&&dis[edge[i].v]>dis[p.se]+nowcost) {DIS[EDGE[I].V]=dis[p.se]+Nowcost; Q.push (Make_pair (-dis[edge[i].v],edge[i].v)); PREPOINT[EDGE[I].V]=p.se; PREEDGE[EDGE[I].V]=i; } } } if(Dis[t]>inf) Break; for(intI=0; i<=n;i++) h[i]+=Dis[i]; intnowflow=INF; for(intnow=t;now!=s;now=Prepoint[now]) Nowflow=min (NOWFLOW,EDGE[PREEDGE[NOW]].F); for(intnow=t;now!=s;now=Prepoint[now]) edge[preedge[now]].f-=Nowflow, Edge[preedge[now]^1].f+=Nowflow; Ansflow+=Nowflow; Anscost+=nowflow*H[t]; } returnMake_pair (ansflow,anscost);}intMain () {#ifdef WIN32 freopen ("a.in","R", stdin); #endifmemset (Head,-1,sizeof(head)); N=read (), M=read (), S=read (), t=read (); for(intI=1; i<=m;i++) { intX=read (), Y=read (), F=read (), z=read (); Addedge (X,Y,F,Z); } Pair ans=Dij (); printf ("%d%d", ans.fi,ans.se); return 0;}
Rokua P3381 "template" Minimum cost maximum flow (Dijstra charge flow)