uva A Spy in the Metro(洛谷 P2583 地铁间谍)
A Spy in the Metro
Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we nd her in the rst station of Algorithms City Metro, examining the time table. The Algorithms City Metro consists of a single line with trains running both ways, so its time table is not complicated.
Maria has an appointment with a local spy at the last station of Algorithms City Metro. Maria knows that a powerful organization is after her. She also knows that while waiting at a station, she is at great risk of being caught. To hide in a running train is much safer, so she decides to stay in running trains as much as possible, even if this means traveling backward and forward. Maria needs to know a schedule with minimal waiting time at the stations that gets her to the last station in time for her appointment. You must write a program that nds the total waiting time in a best schedule for Maria.
The Algorithms City Metro system has N stations, consecutively numbered from 1 to N. Trains move in both directions: from the rst station to the last station and from the last station back to the rst station. The time required for a train to travel between two consecutive stations is xed since all trains move at the same speed. Trains make a very short stop at each station, which you can ignore for simplicity. Since she is a very fast agent, Maria can always change trains at a station even if the trains involved stop in that station at the same time.




Input
The input le contains several test cases. Each test case consists of seven lines with information as follows.
Line 1. The integer N (2 N 50), which is the number of stations.
Line 2. The integer T (0 T 200), which is the time of the appointment.
Line 3. N 1 integers: t1; t2; : : : ; tN 1 (1 ti 20), representing the travel times for the trains between two consecutive stations: t1 represents the travel time between the rst two stations, t2 the time between the second and the third station, and so on.
Line 4. The integer M1 (1 M1 50), representing the number of trains departing from the rst station.
Line 5. M1 integers: d1; d2; : : : ; dM1 (0 di 250 and di < di+1), representing the times at which trains depart from the rst station.
Line 6. The integer M2 (1 M2 50), representing the number of trains departing from the N-th station.
Line 7. M2 integers: e1; e2; : : : ; eM2 (0 ei 250 and ei < ei+1) representing the times at which trains depart from the N-th station.
The last case is followed by a line containing a single zero.
Output
For each test case, print a line containing the case number (starting with 1) and an integer representing the total waiting time in the stations for a best schedule, or the word `impossible' in case Maria is unable to make the appointment. Use the format of the sample output.
Sample Input
4
55
5 10 15
4
0 5 10 20
4
0 5 10 15
4
18
1 2 3
5
0 3 6 10 12
6
0 3 5 7 12 15
2
30
20
1
20
7
1 3 5 7 11 13 17
0
Sample Output
Case Number 1: 5
Case Number 2: 0
Case Number 3: impossible
.思路:时间是单向流逝的这是一个天然的“序”。影响决策的只有当前的时间和所在的车站。
这样我们就有了两种做法:
1.按时间倒序推:f[i][j]表示在时刻i,处在车站j,最少还需要多少时间。边界条件为f[T][n]=0,而其他的f[T][j]为正无穷,则有如下三种决策:
①:等一分钟,f[i][j]=f[i+1][j]+1。
②:如果有,则搭乘向右开的车,f[i][j]=min(f[i][j],f[i+t[j]][j+1])。
③:如果有,则搭乘向左开的车,f[i][j]=min(f[i][j],f[i+t[j-1]][j-1])。
2.按时间正序推:f[i][j]表示在时刻i,处在车站j,最少还需要多少时间。边界条件为f[0][1]=0,而其他的f[0][j]为正无穷,则有如下三种决策:
①:等一分钟,f[i][j]=f[i-1][j]+1。
②:如果有,则搭乘向右开的车,f[i][j]=min(f[i][j],f[i-t[j-1]][j-1])。
③:如果有,则搭乘向左开的车,f[i][j]=min(f[i][j],f[i-t[j]][j+1])。
时间倒序:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int tot,n,T,t[],M1,d1[],M2,d2[];
int f[][],train[][][];
int main(){
while(cin>>n&&n!=){
tot++;
memset(f,,sizeof(f));
memset(train,,sizeof(train));
cin>>T;
for(int i=;i<n;i++) cin>>t[i];
cin>>M1;
for(int i=;i<=M1;i++) cin>>d1[i];
cin>>M2;
for(int i=;i<=M2;i++) cin>>d2[i];
for(int i=;i<=M1;i++){
int time=d1[i];
for(int j=;j<n;j++){
train[time][j][]=;
time+=t[j];
}
}
for(int i=;i<=M2;i++){
int time=d2[i];
for(int j=n;j>;j--){
train[time][j][]=;
time+=t[j-];
}
}
for(int i=;i<n;i++) f[T][i]=;
f[T][n]=;
for(int i=T-;i>=;i--)
for(int j=;j<=n;j++){
f[i][j]=f[i+][j]+;
if(j<n&&train[i][j][]&&i+t[j]<=T)
f[i][j]=min(f[i][j],f[i+t[j]][j+]);
if(j>&&train[i][j][]&&i+t[j-]<=T)
f[i][j]=min(f[i][j],f[i+t[j-]][j-]);
}
cout<<"Case Number"<<" "<<tot<<": ";
if(f[][]>=) cout<<"impossible"<<endl;
else cout<<f[][]<<endl;
}
}
时间正序:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int tot,n,T,t[],M1,d1[],M2,d2[];
int f[][],train[][][];
int main(){
while(cin>>n&&n!=){
tot++;
memset(t,,sizeof(t));
memset(f,0x3f,sizeof(f));
memset(train,,sizeof(train));
cin>>T;
for(int i=;i<n;i++) cin>>t[i];
cin>>M1;
for(int i=;i<=M1;i++) cin>>d1[i];
cin>>M2;
for(int i=;i<=M2;i++) cin>>d2[i];
for(int i=;i<=M1;i++){
int time=d1[i];
for(int j=;j<=n&&time<=T;j++){
train[time][j][]=;
time+=t[j];
}
}
for(int i=;i<=M2;i++){
int time=d2[i];
for(int j=n;j>=&&time<=T;j--){
train[time][j][]=;
time+=t[j-];
}
}
f[][]=;
for(int i=;i<=T;i++)
for(int j=;j<=n;j++){
f[i][j]=min(f[i-][j]+,f[i][j]);
if(train[i][j][])
f[i][j]=min(f[i][j],f[i-t[j-]][j-]);
if(train[i][j][])
f[i][j]=min(f[i][j],f[i-t[j]][j+]);
}
cout<<"Case Number"<<" "<<tot<<": ";
if(f[T][n]<=T) cout<<f[T][n]<<endl;
else cout<<"impossible"<<endl;
}
}
洛谷的相同的题目,或者说是题目翻译。
P2583 地铁间谍
题目描述
特工玛利亚被送到S市执行一个特别危险的任务。她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂。
玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰头。玛利亚知道有一个强大的组织正在追踪她,她知道如果一直呆在一个车站,她会有很大的被抓的风险,躲在运行的列车中是比较安全的。所以,她决定尽可能地呆在运行的列车中,她只能往前或往后坐车。
玛利亚为了能准时且安全的到达最后一个车站与对方碰头,需要知道在在车站最小等待时间总和的计划。你必须写一个程序,得到玛丽亚最短的等待时间。当然,到了终点站之后如果时间还没有到规定的时刻,她可以在车站里等着对方,只不过这个等待的时刻也是要算进去的。
这个城市有n个车站,编号是1-n,火车是这么移动的:从第一个车站开到最后一个车站。或者从最后一站发车然后开会来。火车在每特定两站之间行驶的时间是固定的,我们也可以忽略停车的时间,玛利亚的速度极快,所以他可以迅速上下车即使两辆车同时到站。
输入输出格式
输入格式:
输入文件包含多组数据,每组数据都由7行组成
第1行:一个正整数N(2<=N<=50)表示站的数量
第2行:一个正整数T(0<=T<=200)表示需要的碰头时间
第3行:1-(n-1)个正整数(0<ti<70)表示两站之间列车的通过时间
第4行:一个整数M1(1<=M1<=50)表示离开第一个车站的火车的数量
第5行:M1个正整数:d1,d2……dn,(0<=d<=250且di<di+1)表示每一列火车离开第一站的时间
第6行:一个正整数M2(1<=M2<=50)表示离开第N站的火车的数量
第7行:M2个正整数:e1,e2……eM2,(0<=e<=250且ei<ei+1)表示每一列火车离开第N站的时间
最后一行有一个整数0。
输出格式:
对于每个测试案例,打印一行“Case Number N: ”(N从1开始)和一个整数表示总等待的最短时间或者一个单词“impossible”如果玛丽亚不可能做到。按照样例的输出格式。
输入输出样例
4
55
5 10 15
4
0 5 10 20
4
0 5 10 15
4
18
1 2 3
5
0 3 6 10 12
6
0 3 5 7 12 15
2
30
20
1
20
7
1 3 5 7 11 13 17
0
Case Number 1: 5
Case Number 2: 0
Case Number 3: impossible
说明
第一组样例说明,她0分钟时上车,在3号站下车,立刻坐上(0分始发)15分开的车回去,到2号车站,立刻坐上(20分始发)25开的车到终点,50分到,还需要等待5分钟。
uva A Spy in the Metro(洛谷 P2583 地铁间谍)的更多相关文章
- 洛谷P2583 地铁间谍
P2583 地铁间谍 题目描述 特工玛利亚被送到S市执行一个特别危险的任务.她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂. 玛利亚有一个任务,现在的时间为0,她要从第一个站出发 ...
- 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)
洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...
- 缩点【洛谷P1262】 间谍网络
[洛谷P1262] 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他 ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
- 洛谷 P1262 【间谍网络】
题库 : 洛谷 题号 : 1262 题目 : 间谍网络 link : https://www.luogu.org/problemnew/show/P1262 思路 : 这题可以用缩点的思想来做.先用T ...
- UVA 1025_A Spy in the Metro
[题意](小紫书)一个人从站台1出发,乘车要在时刻T到达站台n,为使在站台等车时间最短,她可以选择乘坐两个方向的列车,并在客车停靠站的时候换车. [分析]每次停站下车时,她都有三种选择,1.原地不动 ...
- UVA A Spy in the Metro
点击打开题目 题目大意: 在一个有n个站台的地铁线路里,给你列车通向每相邻两个车站所花费的时间,从0时刻开始,从1号站出发,要在T这个时间点上,到达n号站,给你m1辆从1开到n的列车及其出发时间,和m ...
- 地铁间谍 洛谷 p2583
题目描述 特工玛利亚被送到S市执行一个特别危险的任务.她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂. 玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰 ...
- 洛谷P1710 地铁涨价 图论
其实是个傻逼题但是我太傻逼了然后就错了无数遍总算A了 觉得不写个题解真是亏了 其实是 之前想了个超时想法 然后还自以为很对?后来看了题解发现还是比较妙的哦 于是就想着那还是发个题解记录下趴quq 正解 ...
随机推荐
- codevs3981动态最大子段和(线段树)
3981 动态最大子段和 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 题目还是简单一点好... 有n个数,a ...
- [Swift通天遁地]四、网络和线程-(14)创建一个Socket服务端
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- async 函数-----------------解决异步操作隧道的亮光
之前也学过,只是没有学好,公司现在用的都是async函数 , 所以决定把它弄懂.最近看了看阮一峰的博客,做下记录. 异步I/O不就是读取一个文件吗,干嘛要搞得这么复杂?异步编程的最高境界,就是根本不用 ...
- @RequestParam 和 @RequestBody 接受的时间格式
这两个接受的时间格式不相同 首先看一下他们的区别 @RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容.(Ht ...
- layout 自适应详解
@{ ViewBag.Title = "人员查找"; ViewBag.LeftWidth = "200px"; ViewBag.MiddleW ...
- 解决Logger在Android Studio 3.1版本无法正常加载tag格式
已经升级到Android Studio 3.1的同学可能会发现一个问题, Logcat中如果短时间出现多条日志tag相同, 只会显示第一条日志的tag, 后面的tag会自动隐藏, 这时com.orha ...
- python自动化测试学习笔记-6redis应用
上次我们学到了redis的一些操作,下面来实际运用以下. 这里我们先来学习一下什么是cookie和session. 什么是Cookie 其实简单的说就是当用户通过http协议访问一个服务器的时候,这个 ...
- 【转】Linux字符转换命令col
转自:http://www.cnblogs.com/ningvsban/p/3725464.html [root@www ~]# col [-xb]选项与参数:-x :将 tab 键转换成对等的空格键 ...
- webHttpBinding+wsHttpBinding+basicHttpBinding的区别 (转)
1. webHttpBinding (web AJAX/JSON)2. wsHttpBinding (ASP.NET client) 3. basicHttpBinding (Silverlight) ...
- Android使用的webcview中带有音乐播放控件,在关闭或分享时处于界面不可见状态下,声音仍在播放的问题解决
一. 问题出现原因 我们在做APP分享时,分享webview加载带有音乐播放控件的网页.当弹出分享界面,webview的网页处于后台状态或关闭该网页时,音乐声仍在播放.出现该类现象使我 ...