Bzoj
Sol
Every day in each city to build a point, how to connect the edge, each time only need to run the last residual network on the maximum flow, so the complexity is not too high.
Code
#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespaceStdintGI () {intx=0, w=1;CharCh=getchar (); while((ch<' 0 '|| Ch>' 9 ') &&ch!='-') Ch=getchar ();if(ch=='-') w=0, Ch=getchar (); while(ch>=' 0 '&&ch<=' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar ();returnW?x:-x;}Const intN =1e5+5;Const intINF =1e9;structedge{intTo,nxt,w;} a[n<<5];intn,m,num,uu[n],vv[n],ww[n],id[Wuyi][2018],tot,s,t,head[n],cnt=1,dep[n],cur[n],flow,ans;queue<int>Q;voidLinkintUintVintW) {a[++cnt]= (edge) {v,head[u],w}; head[u]=cnt; A[++cnt]= (Edge) {U,head[v],0}; head[v]=cnt;}BOOLBFS () {memset (DEP,0,sizeof(DEP)); dep[s]=1; Q.push (S); while(! Q.empty ()) {intU=q.front (); Q.pop (); for(intE=HEAD[U];E;E=A[E].NXT)if(A[e].w&&!dep[a[e].to]) Dep[a[e].to]=dep[u]+1, Q.push (a[e].to); }returnDEP[T];}intDfsintUintf) {if(u==t)returnF for(int&E=CUR[U];E;E=A[E].NXT)if(A[e].w&&dep[a[e].to]==dep[u]+1) {intTmp=dfs (A[e].to,min (a[e].w,f));if(TMP) {a[e].w-=tmp;a[e^1].w+=tmp;returnTMP;} }return 0;}intDinic () {intres=0; while(BFS ()) { for(intI=1; i<=tot;++i) Cur[i]=head[i]; while(intTmp=dfs (S,inf)) res+=tmp; }returnRes;}intMain () {N=gi (); M=gi (); Num=gi (); for(intI=1; i<=m;++i) Uu[i]=gi (), Vv[i]=gi (), Ww[i]=gi (); S=++tot; T=++tot; for(intI=1; i<=n;++i) id[i][0]=++tot; Link (s,id[1][0],num); while(++ans) { for(intI=1; i<=n;++i) Id[i][ans]=++tot,link (Id[i][ans-1],id[i][ans],inf); for(intI=1; i<=m;++i) Link (id[uu[i]][ans-1],id[vv[i]][ans],ww[i]); Link (id[n][ans],t,num); Flow+=dinic ();if(Flow==num) Break; } printf ("%d\n", ans);return 0;}
[BZOJ1570] [JSOI2008] Blue Mary's Trip