Canada Tour

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).

——————————————————————————————————题解

NOCOW里说,中国选手在这场比赛的时候,不知道动态规划是什么(IOI93)然后这一年之后,动态规划在各大竞赛多了起来

dp[i,j]表示两个人从1走到i和从1走到j经过的城市总数,dp[i,j]=dp[j,i]  dp[1,1]=1

dp[i,j]=dp[j,i]=max{dp[i][k]+1} 1=<k<j 同时k->j有路径同时dp[i][k]>0

那么答案是max{dp[k][N]} 要求k->N有路径

这样不会重复,因为如果要重复一定经过某个重复的点,dp[k][k]是不合法的,不会被处理,从而它之后的状态也不会被处理,而所有重复状态一定是两个人都经过这个重复点之后的状态,故而不会重复

这样的更新,相当于固定一个人不动,让另一个人走,走的人不被允许走经过固定这个人的路即可

字符串的处理用map

 /*
ID: ivorysi
LANG: C++
PROG: tour
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#include <stack>
#include <map>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
int n,m;
map<string,int> rec;
string cit,str1,str2;
int f[][],used[],dp[][],ans;
void solve() {
scanf("%d%d",&n,&m);
siji(i,,n) {
cin>>cit;
rec[cit]=i;
}
siji(i,,m) {
cin>>str1>>str2;
f[rec[str1]][rec[str2]]=;
f[rec[str2]][rec[str1]]=;
}
dp[][]=;
siji(i,,n) {
siji(j,i+,n) {
xiaosiji(k,,j) {
if(dp[i][k]> && f[k][j]==) dp[i][j]=max(dp[i][k]+,dp[i][j]);
}
dp[j][i]=dp[i][j];
}
}
ans=;
siji(i,,n) {
if(f[i][n]==) ans=max(ans,dp[i][n]);
}
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("tour.in","r",stdin);
freopen("tour.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}

USACO 5.4 Canada Tour的更多相关文章

  1. USACO Seciton 5.4 Canada Tour(dp)

    因为dp(i,j)=dp(j,i),所以令i>j. dp(i,j)=max(dp(k,j))+1(0<=k<i),若此时dp(i,j)=1则让dp(i,j)=0.(因为无法到达此状态 ...

  2. [洛谷P2747] [USACO5.4]周游加拿大Canada Tour

    洛谷题目链接:[USACO5.4]周游加拿大Canada Tour 题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行, ...

  3. 洛谷 P2747 [USACO5.4]周游加拿大Canada Tour

    P2747 [USACO5.4]周游加拿大Canada Tour 题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直 ...

  4. 洛谷 P2747 [USACO5.4]周游加拿大Canada Tour 解题报告

    P2747 [USACO5.4]周游加拿大Canada Tour 题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直 ...

  5. P2747 [USACO5.4]周游加拿大Canada Tour

    题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直到你到达最东边的城市,再由东向西返回,直到你回到开始的城市.除了旅 ...

  6. 洛谷P2747周游加拿大Canada Tour [USACO5.4] dp

    正解:dp 解题报告: 传送门! 其实这题是我做网络流的时候发现了这题,感觉有点像双倍经验,,,? 但是我还不想写网络流的题解,,,因为网络流24题都还麻油做完,,,想着全做完了再写个总的题解什么的( ...

  7. 洛谷 P2747 Canada Tour 周游加拿大 动态规划

    Description 你赢得了一场航空公司举办的比赛,奖品是一张加拿大机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直到你到达最东边的城市,再由东向西返回,直到你回到开始的城 ...

  8. USACO 5.4 章节

    Canada Tour 题目大意 双向连通图,点从左向右排列, 你需要先从最左的点到最右的点,(过程中只能从左向右走) 然后再从最右的点返回最左的点,(过程中只能从右向左走) 过程中除了最左的点,其它 ...

  9. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

随机推荐

  1. vue 脚手架使用

    1. npm指令 vue init 模板类型   项目名称 如: vue init webpack-simple mydemo 2.进入刚才生产的 文件夹 mydemo cd mydemo 3.初始化 ...

  2. MongoDB - MongoDB CRUD Operations, Bulk Write Operations

    Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...

  3. Redis实战(二)CentOS 7上Redis两种方式持久化

    Redis的持久化之RDB RDB方式是通过快照完成的,当符合一定条件时Redis会自动将内存中的所有数据进行快照并且存储到硬盘上. 进行快照的条件在配置文件中指定,有2个参数构成:时间和改动的键的个 ...

  4. [转载]RSA算法详解

    原文:http://www.matrix67.com/blog/archives/5100 数论,数学中的皇冠,最纯粹的数学.早在古希腊时代,人们就开始痴迷地研究数字,沉浸于这个几乎没有任何实用价值的 ...

  5. python核心编程笔记——Chapter6

    Chapter 6.序列:字符串,列表和元组 这章内容比较多啊,看得比较久,而且题目又难很多. 6.1鉴定一个字符串是否是另外一个字符串的字串,这道题目不是很确定,好像没有直接的判定吧. 直接是否内建 ...

  6. ARC官方文档翻译! - iPhone App开发外包专区 - 威锋论坛 - 威锋网

    CHENYILONG Blog ARC官方文档翻译! - iPhone App开发外包专区 - 威锋论坛 - 威锋网  http://bbs.weiphone.com/read-htm-tid-344 ...

  7. Django外键关系:一对一、一对多,多对多

    1. 一对多 model.py class UserTest(models.Model): name = models.CharField(max_length = 16 ) sex = models ...

  8. C. Connect Three(构造)

    题目链接:http://codeforces.com/contest/1087/problem/C 题目大意:给你三个点的坐标,让你用尽可能少的方块,让这三个点连起来. 具体思路: 我们先对这三个点进 ...

  9. 使用qt写的进制转换器

    没有使用什么数据结构,直接使用qt自带的进制转换函数, 实时出结果,代码在后面的链接中,由于初学qt,好多不会,代码构造就有点乱 截图如下

  10. Ubuntu使用apt-get upgrade升级时出错

    今天在按照常规的sudo apt-get update更新软件列表后,再使用sudo apt-get upgrade升级软件时,出现了以下的错误: 正在设置 linux-image-extra-4.4 ...