Los Angeles P2747 Canada Tour Dynamic Planning

Description

You have won an airline competition and the prize is a round-trip ticket to Canada . The trip starts in the westernmost city where the airline is open, and travels west to east until you reach the easternmost city, and back east to west until you get back to the city where you started. Each city can only be visited once, and the city must be visited twice (at the start and end of the tour) except for the city where the tour starts. You are not allowed to use other companies' routes or use other means of transportation.

Give a list of cities that the airline is open to, and a list of direct routes between the two cities. Find a route that can visit as many cities as possible, this route must meet the above conditions, that is, travel from the first city in the list, visit the last city in the list, and then return to the first city.

Input

Line 1: The number of cities N open to the airline and the number V of direct routes to be listed. N is a positive integer not greater than 100. V is any positive integer.

Lines 2..N+1: Each line includes a city name for which the airline is open. City names are arranged from west to east. There will never be a situation where two cities are on the same longitude. The name of each city is a string of up to 15 bytes consisting of the letters of the Latin alphabet; there are no spaces in the city name.

Lines N+2..N+2+V-1: Each line includes two city names (consisting of the city names in the list above), separated by a space. This represents a direct round-trip flight between the two cities .

Ouput

Line 1: The number M of different cities visited by the best route. If the route cannot be found, output 1.

Sample Input

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

Sample Output

7

HINT

Example explanations: Vancouver , Edmonton, Montreal, Halifax, Toronto, Winnipeg, Calgary, and Vancouver (return to start city, but not count as different cities).

Solution

It looks like a cost flow problem, but if we think about it carefully, it is not difficult to find that dynamic programming can be easily solved, and this problem can also be solved by finding the maximum cycle by floyd. The method of dynamic programming is mainly introduced here.

Similar to floyd, we use the adjacency list method to build the map. Because we need to establish the mapping relationship between the city and the number to build the map, the big guys generally use the map, and the konjac will not be so advanced. .

Since the path is not required to be repeated, we can divide the required path into two sections, that is, two paths starting from the starting point and returning to the starting point. Then suppose there are two people who take these two paths respectively, so as to come up with our state: f [i][j] represents the number of states where one person reaches i and the other person reaches j. The state transition equation can be easily deduced:

 

f[i][j]=max{f[i][k]+1}+1 (k,j have a route and 1<=k<j and f[i][k]>0)

Code

#include <stdio.h>
#include <algorithm>
#include <string.h>
#define N 110
using namespace std;
int f[N][N],map[N][N];
int n,v;
int ans=1;
char a[N],b[N];
void add(int x,int y){map[x][y]=1;map[y][x]=1;}
struct note 
{
    int num;
    char id[ 20 ];
}city[N];
void floyd()
{
    f[1][1]=1;
    for(int i=1;i<n;i++)
        for(int j=i+1;j<=n;j++)
            for(int k=1;k<j;k++)
                if(map[j][k]&&f[i][k]&&f[i][k]+1>f[i][j])
                    f[i][j]=f[j][i]=f[i][k]+1;
     for(int i=1;i<=n;i++)
        if(map[i][n])
            years = max(years,f[i][n]);
}
intmain ()
{
    scanf("%d%d",&n,&v);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",city[i].id+1);
        city[i].num=i;
    }
    for(int i=1;i<=v;i++)
    {
        scanf("%s",a+1);
        scanf("%s",b+1);
        int x,y;
        for(int j=1;j<=n;j++)
        {
            if(strcmp(a+1,city[j].id+1)==0)
                x=city[j].num;
            if(strcmp(b+1,city[j].id+1)==0)
                y=city[j].num;
        }
        add(x,y);
    }
    floyd();
    printf("%d",ans);
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325265829&siteId=291194637