USACO 5.4 Canada Tour
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的更多相关文章
- 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.(因为无法到达此状态 ...
- [洛谷P2747] [USACO5.4]周游加拿大Canada Tour
洛谷题目链接:[USACO5.4]周游加拿大Canada Tour 题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行, ...
- 洛谷 P2747 [USACO5.4]周游加拿大Canada Tour
P2747 [USACO5.4]周游加拿大Canada Tour 题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直 ...
- 洛谷 P2747 [USACO5.4]周游加拿大Canada Tour 解题报告
P2747 [USACO5.4]周游加拿大Canada Tour 题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直 ...
- P2747 [USACO5.4]周游加拿大Canada Tour
题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直到你到达最东边的城市,再由东向西返回,直到你回到开始的城市.除了旅 ...
- 洛谷P2747周游加拿大Canada Tour [USACO5.4] dp
正解:dp 解题报告: 传送门! 其实这题是我做网络流的时候发现了这题,感觉有点像双倍经验,,,? 但是我还不想写网络流的题解,,,因为网络流24题都还麻油做完,,,想着全做完了再写个总的题解什么的( ...
- 洛谷 P2747 Canada Tour 周游加拿大 动态规划
Description 你赢得了一场航空公司举办的比赛,奖品是一张加拿大机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直到你到达最东边的城市,再由东向西返回,直到你回到开始的城 ...
- USACO 5.4 章节
Canada Tour 题目大意 双向连通图,点从左向右排列, 你需要先从最左的点到最右的点,(过程中只能从左向右走) 然后再从最右的点返回最左的点,(过程中只能从右向左走) 过程中除了最左的点,其它 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
随机推荐
- python---基础知识回顾(十)进程和线程(py2中自定义线程池和py3中的线程池使用)
一:自定义线程池的实现 前戏: 在进行自定义线程池前,先了解下Queue队列 队列中可以存放基础数据类型,也可以存放类,对象等特殊数据类型 from queue import Queue class ...
- C/C++中的回调函数
在理解“回调函数”之前,首先讨论下函数指针的概念. 函数指针 (1)概念:指针是一个变量,是用来指向内存地址的.一个程序运行时,所有和运行相关的物件都是需要加载到内存中,这就决定了程序运行时的任何物件 ...
- 关于thinkpad安装win10操作系统
thinkpad预装的是win8或者win10,会有自己的分区方式是GPT,所以会出现两个引导分区. F2进入tinkpad的bios,F12进入启动选项 我们用pe进入后,用分区工具删除两个分区,然 ...
- css文字超出显示省略号
单号: white-space:nowrap; overflow:hidden; text-overflow:ellipsis; 多行: word-break: break-all; text-ove ...
- ETL测试基本知识
转载自: https://www.cnblogs.com/clarke157/p/6383024.html 一.ETL测试的重要性: ETL(Extract-Transform-Load的缩写,即数据 ...
- python类和对象-扩展
1.为类或对象动态创建属性或方法 2.__slots__ = ('name','age'),[实例]只能创建指定的属性属性或方法 ---新式类 3.执行父类的构造函数的不同Father.__init_ ...
- Css3帧动画深入探寻,讲点项目中实际会碰到的问题
先加个副标题XD --如何解决background-size为100%下处理@keyframes 正是在项目中遇到副标题,才引起我更深入的探寻 先略带一下基本的css3动画 css3的动画实现是通过属 ...
- Struts2笔记3--获取ServletAPI和OGNL与值栈
获取ServletAPI: 第一种方式: //在request域中放入属性req,暂且认为getContext()获取的是request域空间,但实际不是 ActionContext.getConte ...
- 配置replica set的常见问题
总有人问起配置ReplicaSet不成功,总结了一下基本上的可能性就几种,检查步骤如下: 假设三台机器的IP分别是 A: 192.168.1.2 a.test.com B:192.168.1.3 b. ...
- ajax代码示例
function loadXMLDoc(idName,url,sendOut) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, ...