【Uva1025 A Spy in the Metro】动态规划
题目描述
某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n。有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开。列车在相邻站台间所需的运行时间是固定的,因为所有列车的运行速度是相同的。在时刻0,Mario从第1站出发,目的在时刻T(0≤T≤200)会见车站n的一个间谍。在车站等车时容易被抓,所以她决定尽量躲在开动的火车上,让在车站等待的时间尽量短。列车靠站停车时间忽略不计,且Mario身手敏捷,即时两辆方向不同的列车在同一时间靠站,Mario也能完成换乘。 【输入格式】 输入文件包含数种情况,每一种情况包含以下7行:
第一行是一个正整数n,表示有n个车站 第二行是为T,表示Mario在时刻T见车站n的间谍 第三行有n-1个整数t1,t2,...,tn-1,其中ti表示地铁从车站i到i+1的行驶时间 第四行为M1,及从第一站出发向右开的列车数目 第五行包含M1个正整数a1,a2,...,aM1,即个列车出发的时间 第六行为M2,及从第一站出发向右开的列车数目 第七行包含M2个正整数b1,b2,...,bM2,即个列车出发的时间。
分析
状态:$ F_{i\ j}$ 表示在\(i\)时刻,人处车站$ j$的最少等待时间
状态转移方程
这一道题我们分为三个决策
- 等1分钟
- 搭乘向右的车
- 搭乘向左的车
等待一分钟:$ F_{i\ j}=F_{i+1 \ j}+1\(
向左走:\) F_{i\ j}=Min F_{i+t_j \ j+1}\(
向右走:\) F_{i\ j}=Min F_{i+t_{j-1} \ j-1}$
AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 55, M = 205;
int t[N], d[N][M];
bool l[N][M], r[N][M];
int main()
{
int n, m, ti, cur, cas = 0;
while(~scanf("%d", &n), n)
{
scanf("%d", &ti);
memset(l, 0, sizeof(l)), memset(r, 0, sizeof(r));
for(int i = 1; i < n; ++i) scanf("%d", &t[i]);
scanf("%d", &m);
for(int i = 1; i <= m; ++i)
{
scanf("%d", &cur);
for(int j = 1; j <= n; ++j)
r[j][cur] = 1, cur += t[j];
}
scanf("%d", &m);
for(int i = 1; i <= m; ++i)
{
scanf("%d", &cur);
for(int j = n; j >= 1; --j)
l[j][cur] = 1, cur += t[j - 1];
}
memset(d, 0x3f, sizeof(d));
d[n][ti] = 0;
for(int j = ti - 1; j >= 0; --j)
{
for(int i = 1; i <= n; ++i)
{
d[i][j]= d[i][j + 1] + 1;
if(l[i][j]) d[i][j] = min(d[i][j], d[i - 1][j + t[i - 1]]);
if(r[i][j]) d[i][j] = min(d[i][j], d[i + 1][j + t[i]]);
}
}
printf("Case Number %d: ", ++cas);
if(d[1][0] > ti) puts("impossible");
else printf("%d\n", d[1][0]);
}
return 0;
}
【Uva1025 A Spy in the Metro】动态规划的更多相关文章
- UVA1025-A Spy in the Metro(动态规划)
Problem UVA1025-A Spy in the Metro Accept: 713 Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...
- UVA1025 A Spy in the Metro —— DP
题目链接: https://vjudge.net/problem/UVA-1025 题解: 详情请看紫书P267. 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写. 递推: #include ...
- Uva1025 A Spy in the Metro
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...
- 题解:UVa1025 A Spy in the Metro
原题链接 pdf 题目大意 给出一张无向图图,求该图的最小瓶颈生成树. 无向图的瓶颈生成树:无向图\(G\)的一颗瓶颈生成树是这样的一颗生成树:它最大的边权值在\(G\)的所有生成树中是最小的.瓶颈生 ...
- 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 ...
- 洛谷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 解题报告
A Spy in the Metro Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug Secr ...
- uva 1025 A Spy int the Metro
https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...
随机推荐
- python取整函数 向上取整 向下取整 四舍五入
向上取整 >>> import math >>> math.ceil(3.5) 4 >>> math.ceil(3.4) 4 >>&g ...
- Centos acme.sh 申请 LetsEncrypt 通配证书
1. 安装 acme.sh 注意:如果需要使用 Standalone Mode请先安装socat# yum intall socat It is recommended to install soca ...
- QueryTable的使用以及错误
1.QuerySeter的filter使用遇到的错误 1.1 Filter里的字段名和操作符要用双下划线."__" 不是" _",否则会被认为成是列名的一部分, ...
- .Net RabbitMQ实战指南——HTTP API接口调用
RabbitMQ Management插件还提供了基于RESTful风格的HTTP API接口来方便调用.一共涉及4种HTTP方法:GET.PUT.DELETE和POST.GET方法一般用来获取如集群 ...
- 「模拟8.23」one递推,约瑟夫
前置芝士约瑟夫问题 这样大概就是板子问题了 考场的树状数组+二分的60分暴力??? 1 #include<bits/stdc++.h> 2 #define int long long 3 ...
- 【dp】10-8题解 vacation
vacations 原题codeforeces round 363 (Div2) c 题目描述 暑假到了, Pb 正在计划他的假期. Pb 准备假期去体育馆锻炼或看电影.但体育馆和电影院都有可能当天不 ...
- DBA入门相关知识介绍
DBA(database administrator):数据库管理员 DBMS(database management system):数据库管理系 ...
- Golang十六进制字符串和byte数组互转
Golang十六进制字符串和byte数组互转 需求 Golang十六进制字符串和byte数组互相转换,使用"encoding/hex"包 实现Demo package main i ...
- js笔记13
1.js操作css样式 div.style.width="100px".在div标签内我们添加了一个style属性,并设定了width值,这种写法会给标签带来大量的style属性, ...
- jQuery筛选选择器
<!DOCTYPE html><html><head> <meta http-equiv="Content-type" conten ...