题意:

有线性的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. 微信诡异的 40029 不合法的oauth_code

    最近几天在做微信公共平台开发,之前一切正常运行着,发布一套程序出去之后,发现时不时的报错! 小总结下问题出现原因:微信oauth2.0 接口说明 第一步:用户同意授权,获取code 在确保微信公众账号 ...

  2. Sublime text 取消记住上一次打开的,这功能太墨迹了!

    比较恨,这sublime text的配置全部都是配置文件. 选择菜单:Preferences->Settings-User,增加配置项 //热退出,其实实现一种模拟没有退出的状态,当程序再次启动 ...

  3. Ubuntu C++环境支持

    问题描述:         在Ubuntu中默认安装有gcc,但是只能编辑C程序,现在希望添加C++环境支持 问题解决:         首先是配置gcc,在ubuntu安装完成已经有gcc了(gcc ...

  4. 【锋利的JQuery-学习笔记】输入框提示语-隐藏/显示

    html <div class="search"> <input type="text" id="inputSearch" ...

  5. 设置Eclipse智能提示

    原地址:http://blog.csdn.net/sz_bdqn/article/details/4956162 今天有点时间,研究了一下MyEclispse的智能感知的功能.刚开始使用它时总是感觉如 ...

  6. 精华阅读第 9 期 |滴滴出行 iOS 客户端架构演进之路

    「架构都是演变出来的,没有最好的架构,只有最合适的架构!」最近,滴滴出行平台产品中心 iOS 技术负责人李贤辉接受了 infoQ 的采访,阐述了滴滴的 iOS 客户端架构模式与演变过程.李贤辉也是移动 ...

  7. 1. what is Lua?

    glue language Lua is a proven, robust language, small.

  8. hdu 2736 Surprising Strings(类似哈希,字符串处理)

    重点在判重的方法,嘻嘻 题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int ...

  9. java基础知识回顾之---java String final类普通方法的应用之字符串数组排序

    /* * 1,给定一个字符串数组.按照字典顺序进行从小到大的排序. * {"nba","abc","cba","zz", ...

  10. Hello World---C/C++

    C #include <stdio.h> void main() { printf("Hello World!\n"); } C++ #include <iost ...