题意:

有线性的n个车站,从左到右编号分别为1~n。有M1辆车从第一站开始向右开,有M2辆车从第二站开始向左开。在0时刻主人公从第1站出发,要在T时刻回见车站n 的一个间谍(忽略主人公的换乘时间)。输出最少的等待时间,如果无解输出impossible。

分析:

d(i, j)表示第i时刻在第j个车站,最少还需要的等待时间。边界是:d(T, n) = 0, d(T, i) = +∞

预处理:

has_train[t][i][0]数组是用来记录t时刻第i个车站是否有向右开的车,类似has_train[t][i][1]记录的是向左开的车。

有3种决策:

  1. 等一分钟
  2. 搭乘向左开的车(如果有的话)
  3. 搭乘向右开的车(如果有的话)

边界没有处理到位,WA了好多次。

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int INF = ;
int has_train[][][], t[], dp[][]; int main(void)
{
#ifdef LOCAL
freopen("1025in.txt", "r", stdin);
#endif int kase = , n, T;
while(scanf("%d", &n) == && n)
{
scanf("%d", &T);
for(int i = ; i < n; ++i) dp[T][i] = INF;
dp[T][n] = ;
memset(has_train, , sizeof(has_train)); for(int i = ; i < n; ++i) scanf("%d", &t[i]);
int m, ti;
scanf("%d", &m);
while(m--)
{
scanf("%d", &ti);
for(int i = ; i < n; ++i)
{
if(ti <= T) has_train[ti][i][] = ;
ti += t[i];
}
}
scanf("%d", &m);
while(m--)
{
scanf("%d", &ti);
for(int j = n-; j >= ; --j)
{
if(ti <= T) has_train[ti][j + ][] = ;
ti += t[j];
}
} for(int i = T - ; i >= ; --i)
{
for(int j = ; j <= n; ++j)
{
dp[i][j] = dp[i+][j] + ;
if(j < n && has_train[i][j][] && i + t[j] <= T)
dp[i][j] = min(dp[i][j], dp[i + t[j]][j+]);
if(j > && has_train[i][j][] && i + t[j-] <= T)
dp[i][j] = min(dp[i][j], dp[i + t[j-]][j-]);
}
} printf("Case Number %d: ", ++kase);
if(dp[][] >= INF) puts("impossible");
else printf("%d\n", dp[][]);
} return ;
}

代码君

UVa 1025 (动态规划) A Spy in the Metro的更多相关文章

  1. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

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

  3. uva 1025 A Spy in the Metro 解题报告

    A Spy in the Metro Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug Secr ...

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

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

  5. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

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

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

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

  8. UVa 1025 A Spy in the Metro(动态规划)

    传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...

  9. UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)

    传送门 参考资料: [1]:算法竞赛入门经典:第九章 DAG上的动态规划 题意: Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过: 其中 M1 辆列车从 1 ...

随机推荐

  1. c++ 格式化printf

    类型为uint64_t的变量,使用printf进行打印时,需要区分操作系统: 64位系统:使用%ld 32位系统:使用%llu #include<stdio.h>#include < ...

  2. Node.js 随记

    http://nodejs.org/  下载并安装 node.js 最新版本 运行cmd,输入npm install xxxxxx 回车,安装扩展模块,如:express,socket.io等 运行c ...

  3. Mac OS X 安装并测试 OpenCV

    1. 安装 打开官网的Linux安装OpenCV的网页,打开这个网页的目的不是按照它所提供的步骤安装OpenCV(因为你会遇到一个坑,下文会提到),而是为了安装一些依赖的包或库. 其中的pkg-con ...

  4. What is the difference between Views and Materialized Views in Oracle?

    aterialized views are disk based and update periodically base upon the query definition. Views are v ...

  5. Kibana

    https://github.com/moonstack/moon-kibana.git

  6. C# get set方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. HUSTOJ(转发)

    来源:http://blog.csdn.net/xiajian2010/article/details/12954855 缘起 大四了,快毕业了,所以想准备点LAMP的知识和经验.刚好实验室里有人在搞 ...

  8. lintcode :Count and Say 报数

    题目: 报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...

  9. *[hackerrank]Volleyball Match

    https://www.hackerrank.com/contests/w1/challenges/volleyball-match 此题不错,首先可以看出是DP,S(x, y)= S(x - 1, ...

  10. ORA-12571 : TNS : 包写入程序失败

    错误原因 解决方案 修改D:/oracle/ora92/network/admin目录下sqlnet.ora,将”NAMES.DEFAULT_DOMAIN =” 这一行用#注释掉,将“SQLNET.A ...