Topic links
I spent two hours in the game with DP.
Read the puzzle is the network flow, feel beaten face.
Knowing this model, the problem is that a template is directly water.
First, to remove the current limit, the mid as the source point, and then a to do a meeting point run the network stream, and then the B to do a meeting point run a network stream. Mdwzszz
(First network stream, divided two times run)
Code
////main.cpp//jisuanke11215////Created by New_life on 16/7/4.//copyright©2016 year chenhuan001. All rights reserved.////network stream, educate me#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;//Network flow SAP template, Complexity O (n^2*m)//Call Init (source point, Sink point, number of points in the graph) before using, then call Add_edge () plus edge//call Getflow to derive the maximum flow#defineN 220#defineM N*n#defineINF 0X3FFFFFstructmax_flow{structNode {intTo,w,next; }EDGE[M]; ints,t; intnn; intCnt,pre[n]; intLv[n],gap[n]; voidInitintSsintTtintnum) {//S=ss;//T=tt;nn = num;//Cnt=0; memset (PRE,-1,sizeof(pre)); } voidAdd_edge (intUintVintW//build two sides at the same time{edge[cnt].to=v; EDGE[CNT].W=W; Edge[cnt].next=Pre[u]; Pre[u]=cnt++; Edge[cnt].to=T; EDGE[CNT].W=0; Edge[cnt].next=Pre[v]; PRE[V]=cnt++; } intSDFsintKintW) {if(k==t)returnW; intf=0; intmi=nn-1; for(intP=pre[k];p!=-1;p =Edge[p].next) { intv=edge[p].to,tw=EDGE[P].W; if(tw!=0) { if(lv[k]==lv[v]+1) { intTmp=sdfs (V,min (tw,w-f)); F+=tmp; EDGE[P].W-=tmp; Edge[p^1].w+=tmp; if(f==w| | LV[S]==NN) Break; } if(LV[V]<MI) mi=Lv[v]; } } if(f==0) {Gap[lv[k]]--; if(gap[Lv[k]]==0) {Lv[s]=nn; } Lv[k]=mi+1; GAP[LV[K]]++; } returnF; } intGetflow () {intsum=0; memset (LV,0,sizeof(LV)); Memset (Gap,0,sizeof(GAP)); gap[0]=nn; while(lv[s]<, N) {Sum+=SDFs (S,inf); } returnsum; }}MXF;intmat[ the][ the];intMainintargcConst Char*argv[]) { intT; CIN>>u; while(t--) { intn,m; CIN>>n>>m; intA,b,mid; CIN>>a>>b>>mid; Memset (Mat,0,sizeof(MAT)); for(intI=0; i<m;i++) { intu,v; CIN>>u>>v; MAT[U][V]= Mat[v][u] =1; } //then you build the network streamMxf.init (0,0,2*n+2); for(intI=1; i<=n;i++) { if(i!=mid) Mxf.add_edge (2(In2*i+1,1); for(intj=i+1; j<=n;j++) { if(Mat[i][j] = =0)Continue; if(j!=mid) Mxf.add_edge (2*i+1,2*j,Ten); if(i!=mid) Mxf.add_edge (2*j+1,2(InTen); }} Mxf.s=2*mid+1; MXF.T=2*a+1; Mxf.getflow (); MXF.T=2*b+1; Mxf.getflow (); //after two streams have been found, the path is sought. intans[ the]; intAnscnt=0; intPP =A; while(pp!=mid) {printf ("%d", pp); for(intp=mxf.pre[2*PP];p!=-1;p =Mxf.edge[p].next) { intv =mxf.edge[p].to; if(MXF.EDGE[P].W = =0)Continue; PP= v/2; Break; }} printf ("%d", mid); PP=b; while(pp!=mid) {ans[anscnt++] =pp; for(intp=mxf.pre[2*PP];p!=-1;p =Mxf.edge[p].next) { intv =mxf.edge[p].to; if(MXF.EDGE[P].W = =0)Continue; PP= v/2; Break; } } for(inti=anscnt-1; i>=0; i--) printf ("%d", Ans[i]); printf ("\ n"); } return 0;}/** 1 5 5 2 4 3 1 2 2 3 3 4 4 5 5 1*/
The transportation network of rookie Logistics (network flow)