usaco5.4.1 Canada Tour
来源:互联网 发布:采样率转换算法 编辑:程序博客网 时间:2024/05/22 11:33
一 原题
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 9VancouverYellowknifeEdmontonCalgaryWinnipegTorontoMontrealHalifaxVancouver EdmontonVancouver CalgaryCalgary WinnipegWinnipeg TorontoToronto HalifaxMontreal HalifaxEdmonton MontrealEdmonton YellowknifeEdmonton 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: tourLANG: C++Compiling...Compile: OKExecuting... 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 automatedcongratulations.
/*ID:maxkibb3LANG: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;}
- usaco5.4.1 Canada Tour
- USACO5.4.1 Canada Tour (tour)
- USACO5.4.1 Canada Tour(tour)
- USACO5.4 周游加拿大 Canada Tour
- 洛谷P2747 [USACO5.4]周游加拿大Canada Tour
- Canada Tour
- 双调TSP问题总结---poj 2677和usaco5.4 canada tour
- usaco Canada Tour
- USACO Section 5.4 Canada Tour
- USACO Canada Tour 解题报告
- usaco 5.4.1 Canada Tour
- USACO 5.4 Canada Tour dp
- USACO Section 5.4 Canada Tour - DP..
- usaco training 5.4.2 Canada Tour 题解
- usaco 5.4 Canada Tour(双线程DP)
- canada goose sale After a weeklong Canadian Tour
- C++&Python——【USACO 5.4.1】——Canada Tour
- Tour
- Yii2 yii2-imagine的使用
- LeetCode题解–200. Number of Islands
- 自定义框架之连接池和事务管理
- JAVA中获得今天是星期几
- pthread_create()函数样例
- usaco5.4.1 Canada Tour
- TableViewCell定制
- Kotlin学习博客和文章
- struts开发简易版(只用struts2)
- Python 1行代码实现文本分类(实战笔记),含代码详细说明及运行结果
- 回调方法介绍之中国好室友篇(Java示例)
- Android Studio使用gradle导出jar包并混淆代码代码记录二
- JAVA,假重写,方法假重写,继承父类时的静态方法假重写,静态方法假重写
- 数据结构 迷宫问题