UVa 1025 (动态规划) A Spy in the Metro
题意:
有线性的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种决策:
- 等一分钟
- 搭乘向左开的车(如果有的话)
- 搭乘向右开的车(如果有的话)
边界没有处理到位,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的更多相关文章
- 【uva 1025】A Spy in the Metro
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 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 ...
- uva 1025 A Spy in the Metro 解题报告
A Spy in the Metro Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug Secr ...
- UVA 1025 -- A Spy in the Metro (DP)
UVA 1025 -- A Spy in the Metro 题意: 一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...
- UVA1025-A Spy in the Metro(动态规划)
Problem UVA1025-A Spy in the Metro Accept: 713 Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...
- 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 ...
- 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)
洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...
- UVa 1025 A Spy in the Metro(动态规划)
传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...
- UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)
传送门 参考资料: [1]:算法竞赛入门经典:第九章 DAG上的动态规划 题意: Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过: 其中 M1 辆列车从 1 ...
随机推荐
- 3640: JC的小苹果 - BZOJ
让我们继续JC和DZY的故事.“你是我的小丫小苹果,怎么爱你都不嫌多!”“点亮我生命的火,火火火火火!”话说JC历经艰辛来到了城市B,但是由于他的疏忽DZY偷走了他的小苹果!没有小苹果怎么听歌!他发现 ...
- 2877: [Noi2012]魔幻棋盘 - BZOJ
DescriptionInput 第一行为两个正整数N,M,表示棋盘的大小. 第二行为两个正整数X,Y,表示棋盘守护者的位置. 第三行仅有一个正整数T,表示棋盘守护者将进行次操作. 接下来N行,每行有 ...
- Jqgrid使用
$('#mygrid').jqGrid('GridUnload'); //保留table元素 $('#mygrid').jqGrid('GridDestroy '); //相当于remove,移除 ...
- 【Ural】【1057】Amount of degrees
数位DP 2009年刘聪<浅谈数位类统计问题> 例题一 从组合数 以及 数位DP的角度都可以做…… 首先转化成求1~n内K进制下只有0.1的数的个数: 考虑K进制下第一个为1的位,剩下的数 ...
- git如何ignore
今天新建了一个项目传到git上,但是每次编译都会有一些无用的文件生成,于是就编写了ignore.但是发现无用.因为你的文件已经上传到服务器了,再编写ignore就无用了,ignore的适用是文件没上传 ...
- Leetcode#143 Reorder List
原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...
- [转载]ASP.NET对路径"xxxxx"的访问被拒绝的解决方法小结
异常详细信息: System.UnauthorizedAccessException: 对路径“D:/temp1/MyTest.txt”的访问被拒绝 在windows 2003下,在运行web ...
- 【Asp.Net MVC】Avoid Mass Assignment in ASP.NET MVC
Mass Assignment Vulnerability in ASP.NET MVC: http://freshbrewedcode.com/joshbush/2012/03/05/mass-as ...
- 【设计模式六大原则1】单一职责原则(Single Responsibility Principle)
http://blog.csdn.net/zhengzhb/article/category/926691/1 图片素材来源,java学习手册 ps.内容为自己整理 定义:不要存在多于一个 ...
- HDU 3397 Sequence operation (区间合并,操作比较多)
费了我一天半的时间,到处debug,后来才发现,主要是建树的时候只在叶子节点对lazy1和lazy2进行初始化了,父节点都没初始化...晕. 具体见代码吧. #include <iostream ...