bzoj 3876 Branch Mission
For each edge (X->y) The cost is Z: s-> y flow is 1 the cost for z means that it must go through this edge
x-> y flow is the inf cost Z means you can move around
For each point x x-> T flow to x the out fee is 0
x-> 1 traffic for the INF fee of 0 corresponding to the initial point in the topic can be returned
Also see a lot of people have only run more than 100 ms This is what clever approach to solve qaq or too naive
1#include <bits/stdc++.h>2 #defineN 3103 #defineM 500004 #defineINF 0x7fffffff5 using namespacestd;6 7InlineintRead ()8 {9 intx=0, f=1;CharCh=GetChar ();Ten while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} One while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} A returnx*F; - } - structEdge the { - intu,v,w; - intnext,c; - }vs[m]; + - intN,ee=1, St[n], from[N],s,t; + intans=0, vis[n],dis[n],p[n]; AQueue <int>Q; atInlinevoidAddedge (intUintVintWintc) - { -vs[++ee].u=u;vs[ee].v=v;vs[ee].w=W; -Vs[ee].next=st[u];st[u]=ee;vs[ee].c=C; - } -InlinevoidInsertintUintVintWintc) in { -Addedge (u,v,w,c); Addedge (V,u,0,-c); to } + BOOLSPFA () - { the for(inti=s;i<=t;i++) dis[i]=inf; * while(!q.empty ()) Q.pop (); $Q.push (S);d is[s]=0; vis[s]=1;Panax Notoginseng while(!q.empty ()) - { the intlx=Q.front (); Q.pop (); + for(intI=st[lx];i;i=vs[i].next) A if(vs[i].w&&dis[lx]+vs[i].c<DIS[VS[I].V]) the { +dis[vs[i].v]=dis[lx]+vs[i].c; - from[vs[i].v]=lx;p[vs[i].v]=i; $ if(!VIS[VS[I].V]) Q.push (VS[I].V), vis[vs[i].v]=1; $ } -vis[lx]=0; - } the //for (int i=s;i<=t;i++) printf ("%d", Dis[i]);p rintf ("\ n"); - returndis[t]!=inf;Wuyi } theInlinevoidMCF () - { Wu intSs=inf; for(intI=t;i!=s;i= from[i]) -ss=min (SS,VS[P[I]].W); About for(intI=t;i!=s;i= from[i]) $ { -vs[p[i]].w-=ss;vs[p[i]^1].w+=SS; -ans+=ss*vs[p[i]].c; - } A } + intMain () the { -n=read (); $s=0; t=n+1; the for(intI=1; i<=n;i++) the { the intk=read (); the for(intj=1; j<=k;j++) - { in intA=read (), b=read (); theInsert (S,a,1, b); the Insert (i,a,inf,b); About } theInsert (I,t,k,0); the if(i!=1) Insert (I,1Inf0); the } + /*printf ("%d\n", ee); - for (int i=2;i<=ee;i++) printf ("%d%d%d\n", vs[i].v,vs[i].w,vs[i].c);*/ the while(SPFA ()) MCF ();Bayiprintf"%d\n", ans); the return 0; the}
View Code
Lower bound minimum cost maximum flow