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 ...
随机推荐
- 2124: 等差子序列 - BZOJ
Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一个整数T,表示组数.下接 ...
- angular入门系列教程1
主题: 一个能够跑起来的页面,神奇的效果,无需一样JS代码! 效果图: 细节: 当然,这里甚至连登陆都没做,只是看到神奇的当输入用户名或者密码的时候,下面的预览区域也会有相应的更改.没有一行的JS代码 ...
- 数字PID控制算法
增量式PID控制算法 量式PID控制算法 2009-07-18 10:33 (转载 出处blog.ednchina.com/tengjingshu )blog.ednchina.com/tengjin ...
- iframe嵌入其他网站,如何自适应高度
终于有一周时间,工作不那么忙了,腾出手来总结下工作过程中学到的知识. 每天遇到新问题,解决新问题,但是却很少有时间去仔细研究下,或者总结下.攒的多了,就得从头捋一遍. 说下iframe自适应高度: 搜 ...
- [设计模式] 21 策略模式 Strategy
在GOF的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化. 策略模 ...
- 被遗忘的Android mipmaps简介
被遗忘的 Android mipmaps 简介 [导读]已经发布的 Android Studio1.1 版本是一个 bug 修复版本.在这个版本中,当你创建工程时一项改变将会吸引你的眼球.工程创建登陆 ...
- Volatile 说明
Java™ 语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量.这两种机制的提出都是为了实现代码线程的安全性.其中 Volatile 变量的同步性较差(但有时它更简单并且开销更低) ...
- StringTokenizer用法
import java.util.StringTokenizer; public class StringTokenizerTest { public static void main(String[ ...
- SPOJ LGLOVE 7488 LCM GCD Love (区间更新,预处理出LCM(1,2,...,n))
题目连接:http://www.spoj.com/problems/LGLOVE/ 题意:给出n个初始序列a[1],a[2],...,a[n],b[i]表示LCM(1,2,3,...,a[i]),即1 ...
- Unique Binary Search Tree II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...