1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include<queue> using namespace std; const int M=50050,N=5050,INF=1<<25; int n,m,st,ed,head[N],cnt=0,pre[N],d[N],c[N],cost=0,inq[N]; struct {int to,next,flow,cap,cost;} e[M<<1]; queue<int> Q; inline int read() { register int x=0,t=1; register char ch=getchar(); while (ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if (ch=='-') t=-1,ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x*t; } void add(int u,int v,int cap,int cost) { e[cnt].next=head[u]; head[u]=cnt; e[cnt].to=v; e[cnt].cost=cost; e[cnt++].cap=cap; } void update(int x,int to,int flow) { for(int i=x;i!=to;i=e[pre[i]^1].to) { e[pre[i]].flow+=flow; e[pre[i]^1].flow-=flow; } } int SPFA(int s,int t) { for(int i=1;i<=n;i++) d[i]=INF,c[i]=0; d[s]=0,c[s]=INF,Q.push(s); while (!Q.empty()) { int x=Q.front(); Q.pop(),inq[x]=0; for(int i=head[x];~i;i=e[i].next) { int to=e[i].to,cost=e[i].cost; if (e[i].flow<e[i].cap&&d[x]+cost<d[to]) { d[to]=d[x]+cost; pre[to]=i; c[to]=min(c[x],e[i].cap-e[i].flow); if (!inq[to]) Q.push(to),inq[to]=1; } } } return c[t]; } int MCMF(int s,int t) { int ret=0,new_flow; while (new_flow=SPFA(s,t)) { ret+=new_flow; cost+=new_flow*d[t]; update(t,s,new_flow); } return ret; } int main() { n=read(),m=read(); st=read(),ed=read(); for(int i=1;i<=n;i++) head[i]=-1; for(int i=1;i<=m;i++) { int u=read(),v=read(); int cap=read(),cost=read(); add(u,v,cap,cost); add(v,u,0,-cost); } int max_flow=MCMF(st,ed); printf("%d %d",max_flow,cost); return 0; }
|
近期评论