Test instructions
Given an aerial map, the vertices represent the city and represent the direct route between the 2 cities. It is now required to find a route that meets the following restrictions and is the most traveled by city.
(1) from the westernmost city, one-way from west to east through a number of cities to reach the east end of the city, and then one-way from east to west to return to the starting point (through several cities).
(2) In addition to the beginning of the city, any city can only access 1 times.
Input File Example
Input.txt
8 9
Vancouver
Yellowknife
Edmonton
Calgary
Winnipeg
Toronto
Montreal
Halifax
Vancouver Edmonton
Vancouver Calgary
Calgary Winnipeg
Winnipeg Toronto
Toronto Halifax
Montreal Halifax
Edmonton Montreal
Edmonton Yellowknife
Edmonton Calgary
Output File Example
Output.txt
7
Vancouver
Edmonton
Montreal
Halifax
Toronto
Winnipeg
Calgary
Vancouver
Analysis
Only I this kind of mentally retarded will want to guarantee from west to east how to break.
All know how to do it ...
This loop is actually equivalent to walking two times from the starting point to the end and then the path does not intersect.
Disjoint this constrains the fact that we cannot separate two paths.
Point can only go once, split, flow is 1, the cost is 1.
And then build the edges according to the original. (Only from west to east, two-way side in fact only one is built)
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7#include <cmath>8#include <map>9 using namespacestd;Ten #defineMAXN 2010 One #defineINF 0XFFFFFFF A -map<string,int>M; - intn,m; the - structnode - { - intX,y,f,o,c,next; +}t[maxn*1010];intLen; - intFIRST[MAXN]; + A intMymin (intXintY) {returnX<y?x:y;} at intMymax (intXintY) {returnX>y?x:y;} - - voidInsintXintYintFintc) - { -T[++len].x=x;t[len].y=y;t[len].f=f;t[len].c=C; -t[len].next=first[x];first[x]=len;t[len].o=len+1; int[++len].x=y;t[len].y=x;t[len].f=0; t[len].c=-C; -t[len].next=first[y];first[y]=len;t[len].o=len-1; to } + - intst,ed; thequeue<int>Q; * intDIS[MAXN],PRE[MAXN],FLOW[MAXN]; $ BOOLINQ[MAXN];Panax Notoginseng BOOLBFS () - { the while(!q.empty ()) Q.pop (); +memset (dis,-1,sizeof(DIS)); Amemset (INQ,0,sizeof(INQ)); theQ.push (ST);d is[st]=0; flow[st]=inf;inq[st]=1; + while(!q.empty ()) - { $ intx=Q.front (); $ for(intI=first[x];i;i=t[i].next)if(t[i].f>0) - { - inty=t[i].y; the if(dis[y]<dis[x]+t[i].c) - {Wuyidis[y]=dis[x]+t[i].c; thepre[y]=i; -flow[y]=mymin (FLOW[X],T[I].F); Wu if(!Inq[y]) - { Aboutinq[y]=1; $ Q.push (y); - } - } - } Ainq[x]=0; Q.pop (); + } the if(dis[ed]==-1)return 0; - return 1; $ } the the voidoutput () the { the for(intI=1; i<=len;i+=2) -printf"%d->%d%d%d\n", t[i].x,t[i].y,t[i].f,t[i].c); inprintf"\ n"); the } the About voidMax_flow () the { the intans=0, sum=0; the while(BFS ()) + { -sum+=dis[ed]*flow[ed]; theans+=flow[ed];Bayi intnow=Ed; the while(now!=St) the { -t[pre[now]].f-=flow[ed]; -t[t[pre[now]].o].f+=flow[ed]; thenow=t[pre[now]].x; the } the } the if(ans<2) printf ("No solution!\n"); - Else the { theprintf"%d\n", sum-2); the }94 } the the stringS[maxn],ss; the BOOLvis[maxn*MAXN];98 voidDfsintXBOOLQQ) About { - if(x==n)return;101 if(x<N)102 {103 if(QQ) cout<<s[x]<<Endl;104DFS (x+n,qq); the if(!QQ) cout<<s[x]<<Endl;106 return;107 }108 for(intI=first[x];i;i=t[i].next)if(t[i].f==0&&!Vis[i])109 { thevis[i]=1;111 DFS (T[I].Y,QQ); the return;113 } the } the the voidInit ()117 {118scanf"%d%d",&n,&m);119 for(intI=1; i<=n;i++) - {121Cin>>S[i];122m[s[i]]=i;123 }124 for(intI=1; i<=m;i++) the {126 intX,y,tt;127Cin>>SS; -x=M[SS];129Cin>>SS; they=M[SS];131 if(x==1&&y==n) ins (X+n,y,2,0); the ElseIns (X+n,y,1,0);133 }134st=1; ed=n+N;135Ins1,1+n,2,1); Ins (N,n+n,2,1);136 for(intI=2; i<n;i++) ins (I,i+n,1,1);137memset (Vis,0,sizeof(Vis));138 }139 $ intMain ()141 {142 init ();143 Max_flow ();144Dfs1,1);145cout<<s[n]<<Endl;146Dfs1,0);147 return 0;148}
View Code
There is no sbj, Sang-xin ...
The longest path length of the test is right, the scheme is too lazy to read, should have no problem it.
2016-11-04 19:39:24
"Network Flow 24 Questions" No.11 (longest disjoint path maximum cost flow for air route problems)