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
| int n,m,S,T; struct edge{int u,v,next;LL flow,w;}e[N]; int pre[MAXN],cur[MAXN],cnt=1; void add(int u,int v,LL flow,LL w){e[++cnt]={u,v,pre[u],flow,w},pre[u]=cnt;}
bool vis[MAXN]; int q[MAXN],h,t; LL dis[MAXN]; bool spfa() { fp(i,1,n)dis[i]=linf; mst(vis,0); h=1,t=0,vis[S]=1,dis[S]=0,q[++t]=S; while(h<=t) { int u=q[(h++)%MAXN]; vis[u]=0; gow(u)if(e[i].flow&&dis[v]>dis[u]+w) { dis[v]=dis[u]+w; if(!vis[v])vis[v]=1,q[(++t)%MAXN]=v; } } return dis[T]<linf; } lpr ans; LL dfs(int u,LL flow) { if(u==T||!flow) return ans.fi+=flow,ans.se+=dis[u]*flow,flow; LL ret=0,d; vis[u]=1; for(int i=cur[u];i;i=e[i].next) { int v=e[i].v;LL w=e[i].w; cur[u]=i; if(e[i].flow&&dis[u]+w==dis[v]&&!vis[v]) { d=dfs(v,min(e[i].flow,flow-ret)); if(d) e[i].flow-=d,e[i^1].flow+=d,ret+=d; if(ret==flow)return ret; } } return ret; } void mcf() { while(spfa()) { mst(vis,0); fp(i,1,n)cur[i]=pre[i]; dfs(S,linf); } } int work() { scanf("%d%d",&n,&m); scanf("%d%d",&S,&T); fp(i,1,m) { int u,v; LL f,w; scanf("%d%d%lld%lld",&u,&v,&f,&w); add(u,v,f,w),add(v,u,0,-w); } mcf(); return printf("%lld %lld\n",ans.fi,ans.se); }
|