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

也就是尽量多坐车,最后输出最少等待时间。

析:这个挺复杂,首先时间是一个顺序,设d(i,j)表示时刻 i 在第 j 个车站,最少还要等待多长时间,那么边界是d(T, n) = 0。

并且有三种决策:

决策一:等着 d[i][j] = d[i + 1][j] + 1; 为什么从i + 1 过来呢? 你想一下,DP表示等待的时间,那么是不是应该倒着来呢?

决策二:有往右行驶的车  d[i][j] = min(d[i][j],  d[i + t[j]][j];

决策三:有往左行驶的车  d[i][j] = min(d[i][j], d[i + t[j - 1]][j]);

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int INF = 0x3f3f3f3f; int main(){
int T, n, t[80], dp[205][60], m1, m2, train[255][55][2], d, k = 0; while(scanf("%d", &n) && n){
scanf("%d", &T);
t[0] = 0;
for(int i = 1; i < n; ++i)
scanf("%d", &t[i]);
scanf("%d", &m1);
memset(train, false, sizeof(train));
for(int i = 0; i < m1; ++i){
scanf("%d", &d);
// train[d][1][0] = true;
int s = d;
for(int j = 0; j < n; ++j){
s += t[j];
if(s <= T) train[s][j+1][0] = true;
else break;
}
} scanf("%d", &m2);
for(int i = 0; i < m2; ++i){
scanf("%d", &d);
train[d][n][1] = true;
int s = d;
for(int j = n-1; j > 1; --j){
s += t[j];
if(s <= T) train[s][j][1] = true;
else break;
}
} for(int i = 1; i < n; ++i) dp[T][i] = INF;
dp[T][n] = 0; for(int i = T-1; i >= 0; --i) /// t
for(int j = 1; j <= n; ++j){ /// g
dp[i][j] = dp[i+1][j] + 1;
if(j < n && train[i][j][0] && i + t[j] <= T)
dp[i][j] = min(dp[i][j], dp[i+t[j]][j+1]);
if(j > 1 && train[i][j][1] && i + t[j-1] <= T)
dp[i][j] = min(dp[i][j], dp[i+t[j-1]][j-1]);
} if(dp[0][1] >= INF) printf("Case Number %d: impossible\n", ++k);
else printf("Case Number %d: %d\n", ++k, dp[0][1]);
}
return 0;
}

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

  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(动态规划)

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

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

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

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

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

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

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

  8. uva 1025 A Spy int the Metro

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

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

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

随机推荐

  1. WF从入门到精通学习目录

    WF从入门到精通(第一章):WF简介 WF从入门到精通(第二章):workflow运行时 WF从入门到精通(第三章):workflow实例 WF从入门到精通(第四章):活动及workflow类型介绍 ...

  2. phpstorm 光标设置

    1.是否允许光标定位在行尾之后的任意位置Allow placement of caret after end of line 这个设置是上下换行的时候,光标可以定位在任意位置,只能通过方向键移动光标, ...

  3. Autocad 2010+ObjectArx 2010 +Vs2010 的.net 开发设置(转)

    Autocad 2010+ObjectArx 2010 +Vs2010 的.net 开发设置 分类: ObjectArx.net2010-09-14 16:52 4203人阅读 评论(7) 收藏 举报 ...

  4. Httpclient 支持https(转)

    参考:https://jingyan.baidu.com/article/154b46317353d228ca8f4112.html 参考:https://www.jianshu.com/p/a444 ...

  5. hadoop启动时,报ssh: Could not resolve hostname xxx: Name or service not known

    本文转载自:http://blog.csdn.net/wodewutai17quiet/article/details/76795951 问题:hadoop启动时,报ssh: Could not re ...

  6. Bootstrap-CL:超大屏幕

    ylbtech-Bootstrap-CL:超大屏幕 1.返回顶部 1. Bootstrap 超大屏幕(Jumbotron) 本章将讲解 Bootstrap 支持的另一个特性,超大屏幕(Jumbotro ...

  7. Unity容器声明周期管理

    Having said that, here is a solution that you can use with the Unity container: Create some custom a ...

  8. JSON格式化工具推荐

    JSON以其独特的简洁方便及与Javscript的无缝集成在WEB2.0时瓦风靡全球.   不过做为开发者,当看到一段很长的未格式化的JSON代码时,你会不会感到头晕? {"meta&quo ...

  9. 配置mysql环境变量

    配置mysql环境变量(非必要) 说明:给mysql配置环境变量后我们就可以在cmd里运行mysql(开启.停止等操作) 1. 和其实环境变量的配置方法一样,我们打开环境变量配置窗口(组合键win+P ...

  10. 小米笔记本pro CPU GPU 做科学计算的算力对比

    小米笔记本pro:15.6寸,i7-8850,16G,256G,GPU:MX150 测试对象Caffe,MNIST训练 使用纯CPU训练: 1.耗时:11分58秒 2.功耗:35W 使用GPU训练: ...