原题

城市里的间谍

分析

动态规划,dp[i][j]表示你在时刻i,车站j,最少还要等待的时间. 边界条件d[T][n]=0 已经到达,其他d[T][i]=inf不可达.

在一个站点时,有以下三种决策:

  1. 等一分钟
  2. 搭乘往左开的车(前提是有)
  3. 搭乘往右开的车

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的更多相关文章

  1. UVa 1025 城市里的间谍

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

  2. 城市里的间谍B901

    城市里的间谍   城市里的间谍 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 某城市的地铁是线性的,有 n(2 <= n ...

  3. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  4. 9-1 A Spy in the Metro uva1025 城市里的间谍 (DP)

    非常有价值的dp题目  也是我做的第一题dp    真的效率好高 题意:某城市的地铁是线性的 有n个车站 从左到右编号为1-n  有m1辆列车从第一站开始往右开 还有m2辆列车从第n站开始往左开  在 ...

  5. 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍

    参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...

  6. UVA1025 城市里的间谍

    #include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...

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

  8. UVA 1025 -- A Spy in the Metro (DP)

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

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

随机推荐

  1. [development][dpdk][hugepage] 为不同的结点分配不同大小的大页内存

    这个事来自dpdk, 所以, 先参考. http://dpdk.org/doc/guides/linux_gsg/sys_reqs.html 当前, 假设你已经读过上边内容, 知道大页内存时候, dp ...

  2. jquery-- json字符串没有自动包装为 json对象

    jquery 的一个坑 页面使用 ajax ,回调函数中获取后端返回的 json 格式数据(ajax 未显式指定返回值类型),后端controller方法标注 @ResponseBody 并返回一个 ...

  3. 《Mysql ALTER基本操作》

    一:ALTER 添加单列 - 语法 - ALTER TABLE 表名 ADD 列名 定义类型 [FIRST(列将加入最上方) | AFTER 字段名(列加入某某字段之后) ] - 示例 `user` ...

  4. Appium入门(3)__ Appium Server安装

    安装Appium 1.下载并安装:https://bitbucket.org/appium/appium.app/downloads/ 2. 系统变量PATH 增加 C:\Program Files ...

  5. Java+Selenium如何解决空指针

    1.问题描述:浏览器获取当期窗口值获取为空.

  6. 【Python基础】Pycharm默认快捷键

    PyCharm常用快捷键和设置 代码快速运行: Alt+Shift+F10  编辑代码的时候经常的要换下一行,但是光标没有在行末,可以用这个命令直接换行: Shift+Enter 行注释/取消行注释: ...

  7. 【Linux】阿里云服务器部署--禅道

    Xshell部署环境 回到Xshell界面,连上阿里云服务器,参考上一篇[linux学习1-Xshell连接阿里云ECS服务器](https://www.cnblogs.com/yoyoketang/ ...

  8. logback logback.xml常用配置详解(二)<appender>

    转自:http://aub.iteye.com/blog/1101260 logback 常用配置详解(二) <appender> <appender>: <append ...

  9. MongoDB pymongo模块 更新数据

    现在chat集合里有3条数据 import pymongo mongo_client = pymongo.MongoClient( host='192.168.0.112', port=27017, ...

  10. Python之路 day1 基础1 变量 for while 用户输入

    一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...