UVa 1025 A Spy in the Metro (DP动态规划)
题意:一个间谍要从第一个车站到第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动态规划)的更多相关文章
- 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 ...
- UVA 1025 -- A Spy in the Metro (DP)
UVA 1025 -- A Spy in the Metro 题意: 一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...
- UVa 1025 A Spy in the Metro(动态规划)
传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...
- World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)
分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代 ...
- uva 1025 A Spy in the Metro 解题报告
A Spy in the Metro Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug Secr ...
- 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 ...
- DAG的动态规划 (UVA 1025 A Spy in the Metro)
第一遍,刘汝佳提示+题解:回头再看!!! POINT: dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间: 终点的状态很确定必然是的 dp[T][N] = 0 ---即在 ...
- uva 1025 A Spy int the Metro
https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...
- UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)
传送门 参考资料: [1]:算法竞赛入门经典:第九章 DAG上的动态规划 题意: Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过: 其中 M1 辆列车从 1 ...
随机推荐
- time函数及其用法
Linux下三种时间 st_atime:文件中的数据最后被访问的时间 Time when file data was last accessed. Changed by the ...
- git Permissions 0777 for '/home/xxx/.ssh/id_rsa' are too open.
使用 git 时出现下面的问题,原因是 git 公钥的权限被修改了. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WAR ...
- 转-在Mac OS上搭建Python的开发环境
在Mac OS上搭建Python的开发环境 本文转载自:http://www.jb51.net/article/76931.htm 一. 安装python mac系统其实自带了一个python的执 ...
- 黄聪:VPS服务器如何配置PHP.ini解决wordpress使用WP-Mail-SMTP插件发邮件出现Could not connect to SMTP host的解决办法
1.首先是WP-Mail-SMTP的下载地址:http://wordpress.org/plugins/wp-mail-smtp/ 2.出现Could not connect to SMTP host ...
- 关于 android 环信无法正确获取昵称的问题
本案例中 username 记录成 userId了, nick 始终为空...,这是 getNick() 取得的就是 username..... 如果想自己取得自己系统的nickname则 做以下调整 ...
- XSS获取cookie并利用
获取cookie利用代码cookie.asp <html> <title>xx</title> <body> <%testfile = Serve ...
- TreeSet函数
TreeSet类的排序问题 TreeSet支持两种排序方法:自然排序和定制排序.TreeSet默认采用自然排序. 1.自然排序 TreeSet会调用集合元素的compareTo(Object ob ...
- 给iOS开发新手送点福利,简述UIActivityIndicatorView的属性和用法
UIActivityIndicatorView 1. activityIndicatorViewStyle 设置指示器的样式 UIActivityIndicatorViewStyleWhiteLa ...
- canvas高级动画示例
canvas高级动画示例 演示地址 https://qzruncode.github.io/example/index.html <!DOCTYPE html> <html lang ...
- OpenCL 直方图
▶ 计算直方图,由原子计数和规约计算两部分组成 ● 最简单的版本,代码 // kernel.cl #pragma OPENCL EXTENSION cl_khr_local_int32_base_at ...