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])。

时间倒序:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int tot,n,T,t[],M1,d1[],M2,d2[];
  7. int f[][],train[][][];
  8. int main(){
  9. while(cin>>n&&n!=){
  10. tot++;
  11. memset(f,,sizeof(f));
  12. memset(train,,sizeof(train));
  13. cin>>T;
  14. for(int i=;i<n;i++) cin>>t[i];
  15. cin>>M1;
  16. for(int i=;i<=M1;i++) cin>>d1[i];
  17. cin>>M2;
  18. for(int i=;i<=M2;i++) cin>>d2[i];
  19. for(int i=;i<=M1;i++){
  20. int time=d1[i];
  21. for(int j=;j<n;j++){
  22. train[time][j][]=;
  23. time+=t[j];
  24. }
  25. }
  26. for(int i=;i<=M2;i++){
  27. int time=d2[i];
  28. for(int j=n;j>;j--){
  29. train[time][j][]=;
  30. time+=t[j-];
  31. }
  32. }
  33. for(int i=;i<n;i++) f[T][i]=;
  34. f[T][n]=;
  35. for(int i=T-;i>=;i--)
  36. for(int j=;j<=n;j++){
  37. f[i][j]=f[i+][j]+;
  38. if(j<n&&train[i][j][]&&i+t[j]<=T)
  39. f[i][j]=min(f[i][j],f[i+t[j]][j+]);
  40. if(j>&&train[i][j][]&&i+t[j-]<=T)
  41. f[i][j]=min(f[i][j],f[i+t[j-]][j-]);
  42. }
  43. cout<<"Case Number"<<" "<<tot<<": ";
  44. if(f[][]>=) cout<<"impossible"<<endl;
  45. else cout<<f[][]<<endl;
  46. }
  47. }

时间正序:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int tot,n,T,t[],M1,d1[],M2,d2[];
  7. int f[][],train[][][];
  8. int main(){
  9. while(cin>>n&&n!=){
  10. tot++;
  11. memset(t,,sizeof(t));
  12. memset(f,0x3f,sizeof(f));
  13. memset(train,,sizeof(train));
  14. cin>>T;
  15. for(int i=;i<n;i++) cin>>t[i];
  16. cin>>M1;
  17. for(int i=;i<=M1;i++) cin>>d1[i];
  18. cin>>M2;
  19. for(int i=;i<=M2;i++) cin>>d2[i];
  20. for(int i=;i<=M1;i++){
  21. int time=d1[i];
  22. for(int j=;j<=n&&time<=T;j++){
  23. train[time][j][]=;
  24. time+=t[j];
  25. }
  26. }
  27. for(int i=;i<=M2;i++){
  28. int time=d2[i];
  29. for(int j=n;j>=&&time<=T;j--){
  30. train[time][j][]=;
  31. time+=t[j-];
  32. }
  33. }
  34. f[][]=;
  35. for(int i=;i<=T;i++)
  36. for(int j=;j<=n;j++){
  37. f[i][j]=min(f[i-][j]+,f[i][j]);
  38. if(train[i][j][])
  39. f[i][j]=min(f[i][j],f[i-t[j-]][j-]);
  40. if(train[i][j][])
  41. f[i][j]=min(f[i][j],f[i-t[j]][j+]);
  42. }
  43. cout<<"Case Number"<<" "<<tot<<": ";
  44. if(f[T][n]<=T) cout<<f[T][n]<<endl;
  45. else cout<<"impossible"<<endl;
  46. }
  47. }

洛谷的相同的题目,或者说是题目翻译。

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”如果玛丽亚不可能做到。按照样例的输出格式。

输入输出样例

输入样例#1:

  1. 4
  2. 55
  3. 5 10 15
  4. 4
  5. 0 5 10 20
  6. 4
  7. 0 5 10 15
  8. 4
  9. 18
  10. 1 2 3
  11. 5
  12. 0 3 6 10 12
  13. 6
  14. 0 3 5 7 12 15
  15. 2
  16. 30
  17. 20
  18. 1
  19. 20
  20. 7
  21. 1 3 5 7 11 13 17
  22. 0
输出样例#1:

  1. Case Number 1: 5
  2. Case Number 2: 0
  3. Case Number 3: impossible

说明

第一组样例说明,她0分钟时上车,在3号站下车,立刻坐上(0分始发)15分开的车回去,到2号车站,立刻坐上(20分始发)25开的车到终点,50分到,还需要等待5分钟。

uva A Spy in the Metro(洛谷 P2583 地铁间谍)的更多相关文章

  1. 洛谷P2583 地铁间谍

    P2583 地铁间谍 题目描述 特工玛利亚被送到S市执行一个特别危险的任务.她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂. 玛利亚有一个任务,现在的时间为0,她要从第一个站出发 ...

  2. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  3. 缩点【洛谷P1262】 间谍网络

    [洛谷P1262] 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他 ...

  4. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

  5. 洛谷 P1262 【间谍网络】

    题库 : 洛谷 题号 : 1262 题目 : 间谍网络 link : https://www.luogu.org/problemnew/show/P1262 思路 : 这题可以用缩点的思想来做.先用T ...

  6. UVA 1025_A Spy in the Metro

    [题意](小紫书)一个人从站台1出发,乘车要在时刻T到达站台n,为使在站台等车时间最短,她可以选择乘坐两个方向的列车,并在客车停靠站的时候换车. [分析]每次停站下车时,她都有三种选择,1.原地不动 ...

  7. UVA A Spy in the Metro

    点击打开题目 题目大意: 在一个有n个站台的地铁线路里,给你列车通向每相邻两个车站所花费的时间,从0时刻开始,从1号站出发,要在T这个时间点上,到达n号站,给你m1辆从1开到n的列车及其出发时间,和m ...

  8. 地铁间谍 洛谷 p2583

    题目描述 特工玛利亚被送到S市执行一个特别危险的任务.她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂. 玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰 ...

  9. 洛谷P1710 地铁涨价 图论

    其实是个傻逼题但是我太傻逼了然后就错了无数遍总算A了 觉得不写个题解真是亏了 其实是 之前想了个超时想法 然后还自以为很对?后来看了题解发现还是比较妙的哦 于是就想着那还是发个题解记录下趴quq 正解 ...

随机推荐

  1. cookie和seesion区别

    cookie 和session 的区别详解 这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie ...

  2. $P2126 Mzc家中的男家丁$

    problem #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  3. BZOJ 4481

    思路: 等比数列求和 (无穷项) +线段树找逆序对 //By SiriusRen #include <bits/stdc++.h> ; ; ],ans; struct Node{int x ...

  4. mongoDB的基本用法

    一.MongoDB初识 什么是MongoDB MongoDB是一个基于分布式文件存储的数据库.由c++语言编写.旨在为web应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库 ...

  5. 生成错误:对路径".dll"的访问被拒绝

    第一步:检查dll所在的目录的访问权限,右键文件夹>属性>安全>设置添加EveryOne用户并将完全控制的权限赋给它. 如果问题还没有解决,请不要一遍遍的重启,看第二步: 第二步:右 ...

  6. CF830A/831D Office Keys

    思路: 问题的关键在于对钥匙按照位置排序之后,最终选择的n个钥匙一定是其中的一个连续的区间. 实现: #include <iostream> #include <cstdio> ...

  7. JS——offset

    1.offsetWidth.offsetHeight返回盒子宽度和高度,包括padding与border,不包括margin 2.offsetLeft.offsetTop返回盒子距离定位盒子的x轴方向 ...

  8. eclipse安装python开发pydev插件

    eclipse安装python开发pydev插件 下载eclipse的python开发插件pydev http://pan.baidu.com/s/1qXHt8pI 下载python.exe,并安装. ...

  9. css特殊效果

    border-radius实现特殊形状 .box{ width: 100px; height: 100px; background: orange; border: 1px solid #000; b ...

  10. 让System.Drawing.Bitmap可以在linux运行

    .net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.li ...