第一遍,刘汝佳提示+题解;回头再看!!!

POINT:

  dp[time][sta];  在time时刻在车站sta还需要最少等待多长时间;

  终点的状态很确定必然是的 dp[T][N] = 0 ---即在T时刻的时候正好达到N站点

  我们可以 从终点的状态往起始的状态转化, 一步步走就可以了。

  has_train[t][i][0];  t时刻在i车站是否有往右开的火车

  has_train[t][i][1];  t时刻在i车站是否有往左开的火车

  1. #include <iostream>
  2. #include <sstream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <cctype>
  10. #include <algorithm>
  11. #include <cmath>
  12. #include <deque>
  13. #include <queue>
  14. #include <map>
  15. #include <stack>
  16. #include <list>
  17. #include <iomanip>
  18. using namespace std;
  19.  
  20. #define INF 0xffffff7
  21. #define maxn 200+10
  22.  
  23. int N, T;
  24. int t[maxn];
  25. int dp[maxn][maxn];//dp[i][j] i时刻在j车站最少等候时间
  26. int has_train[maxn][maxn][];
  27. int main()
  28. {
  29. //freopen("out.txt", "r", stdout);
  30. int kase = ;
  31. int N;
  32. while(scanf("%d", &N) && N)
  33. {
  34. memset(has_train, , sizeof(has_train));
  35. memset(t, , sizeof(t));
  36. kase++;
  37. scanf("%d", &T);
  38. for(int i = ; i < N; i++)
  39. scanf("%d", &t[i]);
  40.  
  41. int M1, M2;
  42. scanf("%d", &M1);
  43. for(int i = ; i <= M1; i++)
  44. {
  45. int start;
  46. scanf("%d", &start);
  47. has_train[start][][] = ;  // 标记初始站台
  48. for(int j = ; j < N; j++)
  49. {
  50. int time = start + t[j];
  51. if(time <= T)
  52. {
  53. has_train[time][j+][] = ;
  54. start += t[j];
  55. }
  56. }
  57. }
  58. scanf("%d", &M2);
  59. for(int i = ; i <= M2; i++)
  60. {
  61. int start;
  62. scanf("%d", &start);
  63. has_train[start][N][] = ;  // 标记初始站台
  64. for(int j = N-; j >= ; j--)
  65. {
  66. int time = start + t[j];
  67. if(time <= T)
  68. {
  69. has_train[time][j][] = ;
  70. //cout << "has[" << time << "][" << j << "][1] " << endl;
  71. start += t[j];
  72. }
  73. }
  74. }
  75. printf("Case Number %d: ", kase);
  76.  
  77. for(int i = ; i < N; i++)
  78. dp[T][i] = INF;
  79. dp[T][N] = ;
  80. for(int i = T-; i >= ; i--)
  81. {
  82. for(int j = ; j <= N; j++)
  83. {
  84. dp[i][j] = dp[i+][j] + ; //第T-1时刻在j车站最少等候时间 = 第T时刻在j车站最少等候时间+1; 即决策一---等一分钟
  85. if(j < N && has_train[i][j][] && i + t[j] <= T) //决策二
  86. {
  87. dp[i][j] = min(dp[i][j], dp[i + t[j]][j+]);
  88. }
  89. if(j > && has_train[i][j][] && i + t[j-] <= T) //决策三
  90. {
  91. dp[i][j] = min(dp[i][j], dp[i + t[j-]][j-]);
  92. }
  93. }
  94. }
  95.  
  96. if(dp[][] >= INF) printf("impossible\n");
  97. else printf("%d\n", dp[][]);
  98. }
  99. return ;
  100. }

DAG的动态规划 (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. UVa 1025 A Spy in the Metro(动态规划)

    传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...

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

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

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

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

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

  9. uva 1025 A Spy int the Metro

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

随机推荐

  1. [iOS UI进阶 - 0] Quiartz2D

    A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...

  2. IntelliJ IDEA 开发工具项目maven管理

    今天自己重新部署一下intellij下的项目开发环境,顺便把maven管理项目jar包的方法梳理一下 (1)首先下载apache-maven-3.0.4版本的maven,我自己解压在D:\maven\ ...

  3. Webstorm2016激活码[ 转]

    转至:http://blog.csdn.net/tingwode2014_/article/details/51063657 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QT ...

  4. Objective-c setObject:forKey:和setValue:forKey:的区别

    setObject:forKey: 是NSMutableDictionary类的方法                               key参数类型可以是任意类型对象           ...

  5. 使用Unity制作游戏关卡的教程(一)

    转自: http://gamerboom.com/archives/74131 作者:Matthias Zarzecki 我正在制作<Looking For Group – The Fork O ...

  6. stack对象与heap对象

    从高地址到低地址,分别是stack,heap,static object,stack地址往下增长,heap地址往上增长.只要记住:stack栈顶地址反而小,就知道往下增长了. 禁止产生堆对象 1.产生 ...

  7. POJ1463:Strategic game(树形DP)

    Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...

  8. 解决使用DevExpress开发错误:未将对象引用设置到对象的实例

    在使用DevExpress是总是会出现一些状况.这次同事在他的机器上调试完成的代码发过来,却出现"未将对象引用设置到对象的实例"的错误,提示是Resources.resx的问题.另 ...

  9. delphi Edit - TActionList

    Edit     TEditCut     TEditCopy     TEditpaste     TEditSelectAll     TEditUndo     TEditDelete   编辑 ...

  10. 架构师之路(39)---IoC框架

    1 IoC理论的背景     我们都知道,在採用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,全部的对象通过彼此的合作,终于实现系统的业务逻辑.   图1:软件系统中耦合的对象 假设 ...