分析:动态规划
状态设定
f[i,j] 为假定的甲乙两人,甲走到第i个城市,乙走到第j个城市时,两人走过的城市数目的和。
初始状态
f[1,1]=1
状态转移方程
f[j,i]=f[i,j]=max{f[i,k]+1}(k到j存在飞机航线,以及f[i,k]>0,就是说存在f[i,k]的走法,1<=k<j
交换甲乙,则肯定有f[j,i]=f[i,j]。
目标结果
由于题中告知必须走到终点才能返回,输出结果一定是max{f[i,N]}(i到N存在飞机航线)。如果没有经过城市数目大于1的可行目标状态,则无法完成这次环游,输出1。
Canada Tour
You have won a contest sponsored by anairline. The prize is a ticket to travel around Canada, beginning in the mostwestern point served by this airline, then traveling only from west to eastuntil you reach the most eastern point served, and then coming back only fromeast to west until you reach the starting city. No city may be visited morethan once, except for the starting city, which must be visited exactly twice(at the beginning and the end of the trip). You are not allowed to use anyother airline or any other means of transportation.
Given a list of cities served by theairline and a list of direct flights between pairs of cities, find an itinerarywhich visits as many cities as possible and satisfies the above conditionsbeginning with the first city and visiting the last city on the list andreturning to the first city.
PROGRAM NAME: tour
INPUT FORMAT
Line 1: The number N of cities served by the airline and the number V of directflights that will be listed. N will be a positive integer not larger than 100.V is any positive integer.
Lines 2..N+1: Each line contains a name ofa city served by the airline. The names are ordered from west to east in theinput file. There are no two cities in the same meridian. The name of each cityis a string of, at most, 15 digits and/or characters of the Latin alphabet;there are no spaces in the name of a city.
Lines N+2..N+2+V-1: Each line contains twonames of cities (taken from the supplied list), separated by a single blankspace. This pair is connected by a direct, two-way airline flight.
SAMPLE INPUT (file tour.in)
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 FORMAT
Line 1: The number M of different citiesvisited in the optimal itinerary. Output 1 if no itinerary is possible.
SAMPLE OUTPUT (file tour.out)
7
Namely: Vancouver, Edmonton, Montreal,Halifax, Toronto, Winnipeg, Calgary, and Vancouver (but that's not a differentcity).
code
/*
ID:
PROG: tour
LANG: C++
*/
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <cstring>
using namespace std;
const int MAXN=110;
map<string,int> flag;
bool relation[MAXN][MAXN];
int f[MAXN][MAXN];
int n,v;
ifstream fi("tour.in");
ofstream fo("tour.out");
void clearit()
{
memset(relation,0,sizeof(relation));
}
void init()
{
fi>>n>>v;
for(int i=1;i<=n;i++)
{
string tempname;
fi>>tempname;
flag[tempname]=i;
}
for(int i=1;i<=v;i++)
{
string name1,name2;
fi>>name1>>name2;
relation[flag[name1]][flag[name2]]=true;
relation[flag[name2]][flag[name1]]=true;
}
}
void doit()
{
int i,j,k;
f[1][1]=1;
for (i=1;i<=n;i++)
{
for (j=i+1;j<=n;j++)
{
f[i][j]=-1;
for (k=1;k<j;k++)
{
if ( f[i][k]>0 &&relation[k][j]&& f[i][k]>f[i][j])
f[i][j]=f[i][k];
}
f[j][i]=++f[i][j];
// j=j;
}
}
}
void outit()
{
int ans=1;
for(int i=1;i<n;i++)
if ((relation[i][n]) && (f[i][n]>ans))
ans=f[i][n];
fo<<ans<<endl;
}
int main()
{
clearit();
init();
doit();
outit();
fi.close();
fo.close();
return 0;
}