UVa-1025城市里的间谍 A Spy in the Metro
原题
分析
动态规划,dp[i][j]表示你在时刻i,车站j,最少还要等待的时间. 边界条件d[T][n]=0 已经到达,其他d[T][i]=inf不可达.
在一个站点时,有以下三种决策:
- 等一分钟
- 搭乘往左开的车(前提是有)
- 搭乘往右开的车
AC代码
#include "bits/stdc++.h"
using namespace std;
const int maxn = 50 + 5;
const int maxt = 200 + 5;
const int INF = 1000000000;
// has_train[t][i][0]表示时刻t,在车站i是否有往右开的火车
int t[maxn], has_train[maxt][maxn][2];
int dp[maxt][maxn];
int main() {
int kase = 0, n, T;
while(cin >> n && n) {
cin >>T ;
int M1, M2, d;
for(int i = 1; i <= n-1; i++) cin >> t[i];
// 预处理,计算has_train数组
memset(has_train, 0, sizeof(has_train));
cin >> M1;
while(M1--) {
cin >> d;//针对每一俩车,更新它到车站的时间
for(int j = 1; j <= n-1; j++) {
if(d <= T) has_train[d][j][0] = 1;
d += t[j];
}
}
cin >> M2;
while(M2--) {
cin >> d;
for(int j = n-1; j >= 1; j--) {
if(d <= T) has_train[d][j+1][1] = 1;
d += t[j];
}
}
// DP主过程
for(int i = 1; i <= n-1; i++) dp[T][i] = INF; //不可达
dp[T][n] = 0; //已达
for(int i = T-1; i >= 0; i--)
for(int j = 1; j <= n; j++) {
dp[i][j] = dp[i+1][j] + 1; // 等待一个单位
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]); // 右
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]); // 左
}
// 输出
cout << "Case Number " << ++kase << ": ";
if(dp[0][1] >= INF) cout << "impossible\n";
else cout << dp[0][1] << "\n";
}
return 0;
}
UVa-1025城市里的间谍 A Spy in the Metro的更多相关文章
- UVa 1025 城市里的间谍
https://vjudge.net/problem/UVA-1025 题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短 ...
- 城市里的间谍B901
城市里的间谍 城市里的间谍 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 某城市的地铁是线性的,有 n(2 <= n ...
- 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)
洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...
- 9-1 A Spy in the Metro uva1025 城市里的间谍 (DP)
非常有价值的dp题目 也是我做的第一题dp 真的效率好高 题意:某城市的地铁是线性的 有n个车站 从左到右编号为1-n 有m1辆列车从第一站开始往右开 还有m2辆列车从第n站开始往左开 在 ...
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...
- UVA1025 城市里的间谍
#include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...
- uva A Spy in the Metro(洛谷 P2583 地铁间谍)
A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangero ...
- 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[DP DAG]
UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...
随机推荐
- [development][PCRE] old PCRE
介绍, man手册 txt版 http://www.pcre.org/original/pcre.txt html版 http://www.pcre.org/original/doc/html/pcr ...
- 加载properties文件的三种方法
源代码: package a.one; import java.io.FileInputStream; import java.io.InputStream; import java.util.Pro ...
- 关于字符串的简单dp
看这道题题目叫做魔族密码多新奇的名字点开是道字符串的dp,思考然后想出lis其实但字符串之间的比对只有循环然后其实循环爆不了,太懒点开了题解发现有人使用stl——cstring的函数了方便多了,借鉴一 ...
- HTML5上传文件显示进度
下面我们使用Html 5的新特性file api实现上传文件,并显示上传文件进度百分比.意图是这样的,当选择文件时,显示当前文件信息.这里我们是结合Asp.net MVC做为服务端,您也可以是其它的服 ...
- java 线程 (二) 线程池
package cn.sasa.demo2; import java.util.concurrent.ExecutorService; import java.util.concurrent.Exec ...
- 洛谷P2633 Count on a tree 主席树
传送门:主席树 解题报告: 传送门! umm这题我还麻油开始做 所以 先瞎扯一波我的想法,如果错了我就当反面教材解释这种典型错误,对了我就不管了QwQ 就直接dfs,在dfs的过程中建树 然后就直接查 ...
- 内核ipc机制
内核版本:linux2.6.22.6 硬件平台:JZ2440 驱动源码 block_ipc_poll_key_int_drv.c : #include <linux/module.h> # ...
- caffe matlab接口编译遇到的问题记录
今天编译的过程中遇到的问题以及查阅到的资料,记录在这里,希望可以帮到其他人. BVLC的caffe源码,如果要编译matlab的接口时,首先需要将makefile.config文件中的matlab的安 ...
- [MySQL优化1]centos中MySQL列出所有表
步骤1 - 连接到MySQL数据库服务器:[root@host]# mysql -u root -pEnter password:******提示登陆成功Welcome to the MySQL mo ...
- dedecms怎样调用指定id文章?
前面我们聊了帝国cms如何调用指定id的文章到首页,作为同行的织梦cms应该也是可以实现的吧?那么,dedecms怎样调用指定id文章呢?使用idlist直接调用指定的ID这样的方法是比较好的.官方给 ...