原题

城市里的间谍

分析

动态规划,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. FW--tomcat bi-laternal https and keytool

    说明:按照本文中以下内容配置https,猫server.xml中clientAuth=false,单向验证的时候,网页中可以访问:当clientAuth=true进行双向验证的时候,网页中不可以访问: ...

  2. 转:JAVA守护线程

    原文地址:https://www.cnblogs.com/wxgblogs/p/5417503.html 详细内容看原文~  ,写的挺好的 在Java中有两类线程:User Thread(用户线程). ...

  3. Linux 命令locate

    原文:https://blog.csdn.net/liang19890820/article/details/53285624 简述 locate 可以很快速的搜寻档案系统内是否有指定的档案.其方法是 ...

  4. 内核block机制

    内核版本:linux2.6.22.6 硬件平台:JZ2440 驱动源码 block_ipc_poll_key_int_drv.c : #include <linux/module.h> # ...

  5. 从EnableJpaRepositories说开去

    1 .spring boot @EnableJpaRepositories( repositoryBaseClass = BaseRepositoryImpl.class, includeFilter ...

  6. iptables编写规则

    [-t 表名]:该规则所操作的哪个表,可以使用filter.nat等,如果没有指定则默认为filter -A:新增一条规则,到该规则链列表的最后一行 -I:插入一条规则,原本该位置上的规则会往后顺序移 ...

  7. linux 查看文件目录大小

    du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件>] [--max-de ...

  8. ThreadLocal(一):Thread 、ThreadLocal、ThreadLocalMap

    一.ThreadLocalMap是ThreadLocal的内部类.Thread持有ThreadLocalMap的引用 Entry类继承了WeakReference<ThreadLocal< ...

  9. InnoDB缓冲池预加载在MySQL 5.7中的正确打开方式

    InnoDB缓冲池预加载在MySQL 5.7中的正确打开方式 https://mp.weixin.qq.com/s/HGa_90XvC22anabiBF8AbQ 在这篇文章里,我将讨论在MySQL 5 ...

  10. javascript 及 vue 中的变量前面的美元符号 $ 是什么意思

    $ 您会注意到,我们将库代理为以美元符号“$”为前缀的属性名. 你可能还看过其他的属性和方法,例如,$refs, $on, $mount等等也都是以”$”开头. 虽然属性名上添加前缀不是必须的,但是这 ...