一 原题
You have won a contest sponsored by an airline. The prize is a ticket to travel around Canada, beginning in the most western point served by this airline, then traveling only from west to east until you reach the most eastern point served, and then coming back only from east to west until you reach the starting city. No city may be visited more than 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 any other airline or any other means of transportation.
Given a list of cities served by the airline and a list of direct flights between pairs of cities, find an itinerary which visits as many cities as possible and satisfies the above conditions beginning with the first city and visiting the last city on the list and returning 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 direct flights 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 of a city served by the airline. The names are ordered from west to east in the input file. There are no two cities in the same meridian. The name of each city is 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 two names of cities (taken from the supplied list), separated by a single blank space. 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 cities visited 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 different city).
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: tour LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4240 KB] Test 2: TEST OK [0.000 secs, 4240 KB] Test 3: TEST OK [0.000 secs, 4240 KB] Test 4: TEST OK [0.000 secs, 4240 KB] Test 5: TEST OK [0.000 secs, 4240 KB] Test 6: TEST OK [0.000 secs, 4240 KB] Test 7: TEST OK [0.000 secs, 4240 KB] Test 8: TEST OK [0.000 secs, 4240 KB] Test 9: TEST OK [0.000 secs, 4240 KB] Test 10: TEST OK [0.000 secs, 4240 KB] Test 11: TEST OK [0.000 secs, 4240 KB] All tests OK.
YOUR PROGRAM ('tour') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.
/*
ID:maxkibb3
LANG:C++
PROB:tour
*/
#include<iostream>
#include<fstream>
#include<cstring>
#include<map>
using namespace std;
const int MAXN = 105;
const int INF = 0x7fffffff;
ifstream fin;
ofstream fout;
int N, V;
map<string, int> City;
bool Graph[MAXN][MAXN];
int Dp[MAXN][MAXN];
void init() {
fin.open("tour.in");
fout.open("tour.out");
fin >> N >> V;
string c;
for(int i = 1; i <= N; i++) {
fin >> c;
City[c] = i;
}
string c1, c2;
for(int i = 0; i < V; i++) {
fin >> c1 >> c2;
int i1 = City[c1];
int i2 = City[c2];
Graph[i1][i2] = Graph[i2][i1] = true;
}
}
void solve() {
Dp[1][1] = 1;
for(int i = 1; i < N; i++) {
for(int j = i + 1; j <= N; j++) {
Dp[i][j] = -INF;
for(int k = 1; k < j; k++) {
if(!Graph[k][j]) continue;
if(Dp[i][k] != 0 && Dp[i][k] + 1 > Dp[i][j])
Dp[i][j] = Dp[i][k] + 1;
}
Dp[j][i] = Dp[i][j];
}
}
int ans = 1;
for(int i = 1; i < N; i++) {
if(Graph[i][N] && Dp[i][N] > ans)
ans = Dp[i][N];
}
fout << ans << endl;
}
int main() {
init();
solve();
return 0;
}

本文介绍了一个路径规划问题,目标是从最西端城市出发到达最东端城市再返回起点,途中尽可能多地访问不同城市,且除起点外每个城市仅访问一次。通过动态规划方法解决了这一问题,并给出了完整的代码实现。

704

被折叠的 条评论
为什么被折叠?



