传送门

Description

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first 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 finds 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 first station to the last station and from the last station back to the first station. The time required for a train to travel between two consecutive stations is fixed 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 file 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 first 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 first station. Line 5. M1 integers: d1, d2, . . . , dM1 (0 ≤ di ≤ 250 and di < di+1), representing the times at which trains depart from the first 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

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

思路

  题意:

  某城市的地铁是线性的,有n(2≤n≤50)个车站,从左到右的编号为1~N.有M1辆列车从第一站开始往右开,还有M2辆列车从第n站开始往左开。在时刻0,Mario从第1站出发,目的是在时刻T(0≤T≤200)会见车站n的一个间谍。在车站等车容易被抓,所以她决定尽量躲在开动的火车上,让在车站等待的总时间尽量短。列车靠站停车时间忽略不计,且Mario身手敏捷,即使两辆方向不同的列车在同一时间靠站,Mario也能完成换乘。求最少等待时间。

  思路:

  Mario在某个状态都有三种决策:

    • 等一分钟。
    • 搭乘往右开的车(如果有)
    • 搭乘往左开的车(如果有)

  影响到当前决策的只有当前时间和所处的车站,所以可以用d(i,j)表示时刻i,你在车站j,最少还需要等待多长时间。边界条件d(T,n) = 0,其他d(T,j)为正无穷。

 

  1. /*
  2. *dp[i][j]表示时刻i,在车站j,最少还需要等待多长时间
  3. *has_train[t][i][0]表示时刻t,在车站i是否有向右开的火车
  4. *has_train[t][i][1]表示时刻t,在车站i是否有向左开的火车
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<cstring>
  9. #include<algorithm>
  10. using namespace std;
  11. const int maxn = 55;
  12. const int maxt = 205;
  13. const int INF = 0x3f3f3f3f;
  14. int t[maxn],has_train[maxt][maxn][2],dp[maxt][maxn];
  15. int main()
  16. {
  17. int n,Case = 0;
  18. while (~scanf("%d",&n) && n)
  19. {
  20. int T,M1,M2,d;
  21. memset(has_train,0,sizeof(has_train));
  22. memset(dp,0,sizeof(dp));
  23. scanf("%d",&T);
  24. for (int i = 1;i < n;i++) scanf("%d",&t[i]);
  25. scanf("%d",&M1);
  26. while (M1--)
  27. {
  28. scanf("%d",&d);
  29. for (int j = 1;j < n;j++)
  30. {
  31. if (d <= T) has_train[d][j][0] = 1;
  32. d += t[j];
  33. }
  34. }
  35. scanf("%d",&M2);
  36. while (M2--)
  37. {
  38. scanf("%d",&d);
  39. for (int j = n - 1;j > 0;j--)
  40. {
  41. if (d <= T) has_train[d][j+1][1] = 1;
  42. d += t[j];
  43. }
  44. }
  45. for (int i = 1;i < n;i++) dp[T][i] = INF;
  46. dp[T][n] = 0;
  47. for (int i = T - 1;i >= 0;i--)
  48. {
  49. for (int j =1;j <= n;j++)
  50. {
  51. dp[i][j] = dp[i+1][j] + 1;
  52. if (j < n && has_train[i][j][0] && i + t[j] <= T) dp[i][j] = min(dp[i][j],dp[i+t[j]][j+1]);
  53. if (j > 1 && has_train[i][j][1] && i + t[j-1] <= T) dp[i][j] = min(dp[i][j],dp[i+t[j-1]][j-1]);
  54. }
  55. }
  56. printf("Case Number %d: ",++Case);
  57. dp[0][1]>=INF ? printf("impossible\n"):printf("%d\n",dp[0][1]);
  58. }
  59. return 0;
  60. }

  

UVa 1025 A Spy in the Metro(动态规划)的更多相关文章

  1. UVA - 1025 A Spy in the Metro[DP DAG]

    UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...

  2. UVA 1025 -- A Spy in the Metro (DP)

     UVA 1025 -- A Spy in the Metro  题意:  一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...

  3. uva 1025 A Spy in the Metro 解题报告

    A Spy in the Metro Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug Secr ...

  4. UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)

    传送门 参考资料: [1]:算法竞赛入门经典:第九章 DAG上的动态规划 题意: Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过: 其中 M1 辆列车从 1 ...

  5. DAG的动态规划 (UVA 1025 A Spy in the Metro)

    第一遍,刘汝佳提示+题解:回头再看!!! POINT: dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间: 终点的状态很确定必然是的 dp[T][N] = 0 ---即在 ...

  6. World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)

    分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代 ...

  7. UVa 1025 A Spy in the Metro (DP动态规划)

    题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...

  8. UVA 1025 A Spy in the Metro 【DAG上DP/逆推/三维标记数组+二维状态数组】

    Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After s ...

  9. uva 1025 A Spy int the Metro

    https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...

随机推荐

  1. Oracle 常用函数

    主要是对项目中用过的 oracle 函数进行总结,并做出目录,方便后续项目是快速查找,提高效率. 01.Round (数值的四舍五入) 描述:传回一个数值,该数值是按照指定的小数位元数进行四舍五入运算 ...

  2. sql server 创建只读帐号

    有时候为了方便查询一下数据,会创建个只读帐号,以免误写sql语句改了数据 步骤:用sa帐号连接后,安全性--登录名--新建 输入要新建的帐号密码,在服务器角色里面单勾一个public 在 用户映射里面 ...

  3. PHP核心编程知识点

    一.PHP基本语法 PHP标记:一共有四种,只推荐使用第一种 语句结束符:分号 注释:行注释(//  #)和块注释(/*   */),注释的规范 二.常见的输出语句 print echo var_du ...

  4. C# 7.0 新特性4: 返回引用

    本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...

  5. word-wrap,word-break和white-space总结

    最近网页布局中遇到得比较多,所以打算总结总结. word-wrap: 1.normal(使用浏览器默认的换行规则) 2.break-word(内容将在边界内换行,但是英文换行会按词断句) word-b ...

  6. BOP 2016 复赛题目

    复赛采用团队赛的形式,基于Azure云计算平台在实际大数据的基础上展开前沿课题的较量. 在编程之美挑战赛复赛中,选手需要通过组队共同完成复赛题,考查选手编程能力的同时,也考验选手的团队协作能力.选手点 ...

  7. RabbitMQ 主题(Topic)

    我们进步改良了我们的日志系统.我们使用direct类型转发器,使得接收者有能力进行选择性的接收日志,,而非fanout那样,只能够无脑的转发. 虽然使用direct类型改良了我们的系统,但是仍然存在一 ...

  8. extjs的一些简单动画1

    Ext.Element 类也定义了部分动画函数.我们先来看看Ext.Fx 类中的重要方法. 1.slideIn ( [String anchor], [Object options] ): 功能:滑入 ...

  9. 转 浅谈算法和数据结构: 十 平衡查找树之B树

    前面讲解了平衡查找树中的2-3树以及其实现红黑树.2-3树种,一个节点最多有2个key,而红黑树则使用染色的方式来标识这两个key. 维基百科对B树的定义为"在计算机科学中,B树(B-tre ...

  10. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...