[BZOJ1570] [JSOI2008] Blue Mary's Trip

Source: Internet
Author: User

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&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.