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 ...
随机推荐
- OpenGL教程和书籍
1.http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/
- 【转】escape()、encodeURI()、encodeURIComponent()区别详解
escape().encodeURI().encodeURIComponent()区别详解 原文链接:http://www.cnblogs.com/tylerdonet/p/3483836.html ...
- Shell脚本交互之:自动输入密码
Shell脚本交互之:自动输入密码 2016年04月09日 19:41:26 zhangjikuan 阅读数:58208 标签: Shell交互自动输入密码expect重定向管道 更多 个人分类: A ...
- LeetCode 500 Keyboard Row 解题报告
题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...
- linux 搭建ftp服务
一. 安装ftp yum -y install vsftpd 二.配置 安装完之后在/etc/vsftpd/路径下会存在三个配置文件. vsftpd.conf: 主配置文件 ftpusers: 指定哪 ...
- 新建oracle用户
1.oracle 账户登录linux:2.如果存在多个切换实例:命令:export ORACLE_SID=实例名:如:export ORACLE_SID=utf81863.切换至管理员账户:sqlpl ...
- You-Get——基于Python3的媒体下载工具
You-Get是一个基于 Python 3 的下载工具.使用 You-Get 可以很轻松的下载到网络上的视频.图片及音乐. 项目主页:https://github.com/soimort/you-ge ...
- UVA 1379 - Pitcher Rotation(DP + 贪心)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4125" rel="nofo ...
- jmeter报错:内存溢出
使用jmeter进行压力测试时,经常会遇到内存溢出错误: 2018-08-28 09:01:26,686 ERROR o.a.j.JMeter: Uncaught exception: java.la ...
- Python3学习之路~2.10 修改haproxy配置文件
需求: .查 输入:www.oldboy.org 获取当前backend下的所有记录 .新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 's ...