Network Flow 24 Questions-air route problem

Source: Internet
Author: User

Air route Problems

Time limit 1000MS/128MB

Title Description

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.

For a given aerial diagram, try to design an algorithm to find the best air travel route to meet the requirements.

Input output Format input format:

The 1th line has 2 positive integers N and v,n for the city number, and N<100,v represents the number of direct routes.

Each row in the next N row is a city name that can be accessed by air. The order in which the city names appear is from west to east. That is, the i,j is the order in which cities appear in the City table column, when I>j, the city I in the east of the city J, and there will not be 2 cities on the same meridian. The city name is a string of not more than 15, and the characters in the string can be letters or Arabic numerals. For example, AGR34 or BEL4.

Then in the next V line, each row has 2 city names, separated by a space, such as city1 city2 means city1 to City2 has a straight route, from City2 to City1 also has a direct route.

Output format:

The 1th line is the total number of cities visited in the travel route M. The next m+1 line is the city name of the tour route, with 1 city names per line. The first is the departure city name, and then the other city names are listed in the order of visits. Note that the city name of the last 1 rows (destination city) must be the departure city name. If the problem is not solved, output "no solution!".

Input and Output Sample input example:
8 9VancouverYellowknifeEdmontonCalgaryWinnipegTorontoMontrealHalifaxVancouver Edmontonvancouver Calgarycalgary Winnipegwinnipeg torontotoronto halifaxmontreal Halifaxedmonton Montrealedmonton YellowknifeEdmonton Calgary
Sample output:
Description

Title Link: https://www.luogu.org/problemnew/show/P2770

The maximum right disjoint path. At first I did not think unexpectedly can be seen from the left end of the beginning of the two disjoint path, blind for a long time ...

#include <bits/stdc++.h>#defineINF LLONG_MAX/2#defineN 205using namespacestd;structss{intU,v,next; Long LongFlow,cost;}; SS Edg[n*N];intHead[n],now_edge=0;voidAddedge (intUintVLong LongFlowLong LongCost ) { Cost=-Cost ; Edg[now_edge]=(ss) {u,v,head[u],flow,cost}; Head[u]=now_edge++; Edg[now_edge]= (ss) {V,u,head[v],0,-Cost }; HEAD[V]=now_edge++;}intSPFA (intSintTLong Long&flow,Long Long&Cost ) {    intvis[n]={0}; Vis[s]=1; Queue<int>Q;    Q.push (s); Long LongDis[n];  for(intI=0; i<n;i++) dis[i]=INF; Dis[s]=0; intpre[n]={0}; Long Longaddflow[n]={0}; Addflow[s]=INF;  while(!Q.empty ()) {        intnow=Q.front ();        Q.pop (); Vis[now]=0;  for(inti=head[now];i!=-1; i=Edg[i].next) {SS E=Edg[i]; if(e.flow>0&&dis[e.v]>dis[now]+e.cost) {DIS[E.V]=dis[now]+E.cost; PRE[E.V]=i; ADDFLOW[E.V]=min (E.flow,addflow[now]); if(!VIS[E.V])                    {Q.push (E.V); VIS[E.V]=1; }            }        }    }        if(Dis[t]==inf)return 0; Flow+=Addflow[t]; Cost+=addflow[t]*Dis[t]; intnow=T;  while(now!=s) {Edg[pre[now]].flow-=Addflow[t]; Edg[pre[now]^1].flow+=Addflow[t]; now=edg[pre[now]].u; }    return 1; }voidMCMF (intSintTLong Long&flow,Long Long&Cost ) {     while(SPFA (S,t,flow,cost));}voidinit () {Now_edge=0; memset (Head,-1,sizeof(head));} Map<string,int>Map1;map<int,string>Map2;vector<int>ans;intN;voidDfsintx) {    if(x==n)return; intu=x*2;  for(inti=head[u];i!=-1; i=Edg[i].next) {        if(edg[i^1].flow&&edg[i].v!=u-1) {Edg[i^1].flow--; Ans.push_back (EDG[I].V/2+1); DFS (EDG[I].V/2+1); return; }    }}intMain () {init (); intm; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++)    {        stringA; CIN>>A; Map1.insert (Pair<string,int>(a,i)); Map2.insert (Pair<int,string>(I,a)); }    ints=2*n+1, t=s+1;  while(m--)    {        stringb; CIN>>a>>b; intU=min (Map1[a],map1[b]), v=Max (map1[a],map1[b]); Addedge (U*2, v*2-1Inf0); }         for(intI=2; i<n;i++) Addedge (i*2-1, i*2,1,1); Addedge (1,2,2,1); Addedge (2*n-1,2*n,2,1); Addedge (s),1,2,0); Addedge (2*n,t,inf,0); Long Longflow=0, cost=0;        MCMF (S,t,flow,cost); if(flow<2) {printf ("No solution!\n"); return 0; } printf ("%lld\n",-cost-2); DFS (1); cout<<map2[1]<<Endl;  for(intI=0; I<ans.size (); i++) cout<<map2[ans[i]]<<Endl;    Ans.clear (); DFS (1);  for(intI=ans.size ()-2; i>=0; i--) cout<<map2[ans[i]]<<Endl; cout<<map2[1]<<Endl; return 0; }
View Code

Network Flow 24 Questions-air route problem

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.