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保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
随机推荐
- Hadoop生态圈-Hbase的API常见操作
Hadoop生态圈-Hbase的API常见操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- SQL语句(二十)—— 数据库安全性
数据库安全性 1. SQL Server 配置管理器 => 网络配置 MSSQLSERVER 协议,如果应用程序和SQL Server 在同一机器上,仅使用 Shared Memory (共享 ...
- 苏宁OLAP架构设计
一. 功能综述 OLAP引擎为存储和计算二合一的引擎,自身内部涵盖了对数据的管理以及提供查询能力.底层数据完全规划在引擎内部,外部系统不允许直接操作底层数据,而是需要通过暴露出来的接口来读写引擎内部数 ...
- Vue 的style绑定显示background-image
data () { return { img: require('你的json资源路径') } } :style="{backgroundImage: 'url(' + img + ')'} ...
- Java并发编程原理与实战十八:读写锁
ReadWriteLock也是一个接口,提供了readLock和writeLock两种锁的操作机制,一个资源可以被多个线程同时读,或者被一个线程写,但是不能同时存在读和写线程. 基本规则: 读读不互斥 ...
- MySQL中JSON字段的使用技巧
mysql5.7.8之后开始原生支持json. 在类似mongodb这种nosql数据库中,json存储数据是非常自然的, 在mysql中合理的使用json,能够带来极大的便利 Json字段的使用场景 ...
- 38、使用IO流进行文件拷贝
使用IO流进行文件拷贝 需求:在项目的根目录里面创建一个java.txt的文件,然后将这个文件拷贝到file文件夹里面并且重命名为good.txt文件先以流的方式将java.txt文件读取到内存中,然 ...
- js实现数组、对象深度克隆的两种办法
1.深度克隆的原理 JS中的深度克隆,指的是原对象改变了,克隆出来的新对象也不会改变,原对象与新对象是完全独立的关系. 实现深度克隆的原理得从对象是一种引用类型说起 众所周知,对象是一种引用类型,对象 ...
- linux键盘input_event浅析【转】
转自:http://blog.csdn.net/tdstds/article/details/18710965 input_event(mxckbd_dev, EV_KEY, mxckpd_keyco ...
- sleep允许休眠, delay不允许
在Linux Driver开发中,经常要用到延迟函数:msleep,mdelay/udelay. 虽然msleep和mdelay都有延迟的作用,但他们是有区别的. 1.)对于模块本身 mdelay是忙 ...