UVa-1025城市里的间谍 A Spy in the Metro
原题
分析
动态规划,dp[i][j]
表示你在时刻i,车站j,最少还要等待的时间. 边界条件d[T][n]=0
已经到达,其他d[T][i]=inf
不可达.
在一个站点时,有以下三种决策:
- 等一分钟
- 搭乘往左开的车(前提是有)
- 搭乘往右开的车
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的更多相关文章
- UVa 1025 城市里的间谍
https://vjudge.net/problem/UVA-1025 题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短 ...
- 城市里的间谍B901
城市里的间谍 城市里的间谍 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 某城市的地铁是线性的,有 n(2 <= n ...
- 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)
洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...
- 9-1 A Spy in the Metro uva1025 城市里的间谍 (DP)
非常有价值的dp题目 也是我做的第一题dp 真的效率好高 题意:某城市的地铁是线性的 有n个车站 从左到右编号为1-n 有m1辆列车从第一站开始往右开 还有m2辆列车从第n站开始往左开 在 ...
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...
- UVA1025 城市里的间谍
#include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...
- 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 ...
- UVA 1025 -- A Spy in the Metro (DP)
UVA 1025 -- A Spy in the Metro 题意: 一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...
- 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 ...
随机推荐
- [daily][ulimit][coredump] 快速简单粗暴的用coredump调试
http://www.cnblogs.com/hugetong/p/6898608.html 一个程序挂掉了, 怎么办? 启动coredump 写一个脚本: [root@T185 ~]# cat / ...
- LeetCode 349 Intersection of Two Arrays 解题报告
题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...
- LeetCode 412 Fizz Buzz 解题报告
题目要求 Write a program that outputs the string representation of numbers from 1 to n. But for multiple ...
- easyui中多级表头,主表头不能添加field字段,否则不居中
<th field="" width="120" align="center" align="center" co ...
- idea 在tomcat启动的时候发现控制台输出的是乱码
先前觉得是tomcat的问题,后来发现是idea的问题: 打开IntelliJ IDEA本地安装目录中bin文件夹下的idea.exe.vmoptions和idea64.exe.vmoptions这两 ...
- django2.0内置分页
#导入分页器from django.core.paginator import Paginator 1视图逻辑 #读取数据库 res = Product.objects.all() #建立分页器对象 ...
- CloudStack+KVM环境搭建(步骤很详细,说明ClockStack是用来管理虚拟机的)
文章目录环境准备配置本地域名解析关闭selinux安装ntp服务安装管理端安装Mysql数据库安装服务端RPM:初始化CloudStack数据库:初始化cloudstack管理服务器安装系统虚拟机安装 ...
- Python中Mock的示例(转)
原文:https://segmentfault.com/a/1190000008753754 一些常用的mock示例 先简单定义个类,方便举例: class Person: def __init__( ...
- 【托业】【新托业TOEIC新题型真题】学习笔记13-题库四-P7
>counterpart 对应的人 >Master of Business Administration 工商管理学硕士 >superb 极好的 >executive 执行总监 ...
- OpenFace的一些了解
1.OpenFace内4个样例代码 配置学习了两个 其一: Ubantu 基本命令 Docker 安装方式.发布网站方式.查看验证安装结果命令 Openface 基本demo 实现方式.和基本原理 其 ...