UVA1025-A Spy in the Metro(动态规划)
Accept: 713 Submit: 6160
Time Limit: 3000 mSec
Problem Description

Input

Output
Sample Input
55
5 10 15
4
0 5 10 20
4
0 5 10 15
4
18
1 2 3
5
0 3 6 10 12
6
0 3 5 7 12 15
2
30
20
1
20
7
1 3 5 7 11 13 17
0
Sample Output
Case Number 1: 5
Case Number 2: 0
Case Number 3: impossible
题解:很明显的动态规划,dp[i][j]表示i时刻在j站点还需要的最短等待时间,总共就三种选择,状态转移方程很简单,边界一直是我写动态规划题比较头疼的地方,不过这个题还比较简单,t时刻在n站点自然是0,在其他站点就是INF(为了不会从这些状态转移过去)。
#include <bits/stdc++.h> using namespace std; const int maxt = + , maxn = + ;
const int INF = 0x3f3f3f3f; int read() {
int q = ; char ch = ' ';
while (ch<'' || ch>'') ch = getchar();
while ('' <= ch && ch <= '') {
q = q * + ch - '';
ch = getchar();
}
return q;
} int n, t, m1, m2;
int ti[maxn];
int dp[maxt][maxn];
bool have_train[maxt][maxn][]; void init() {
memset(have_train, false, sizeof(have_train));
for (int i = ; i <= n - ; i++) {
dp[t][i] = INF;
}
dp[t][n] = ;
} int T = ; int main()
{
//freopen("input.txt", "r", stdin);
while (scanf("%d", &n) && n) {
t = read();
init();
for (int i = ; i < n; i++) {
ti[i] = read();
}
ti[n] = ti[] = INF;
m1 = read();
int d;
for (int i = ; i < m1; i++) {
d = read();
int cnt = ;
for (int j = ; j <= n; j++) {
have_train[d][cnt][] = true;
//printf("d:%d cnt:%d\n", d, cnt);
cnt++;
d += ti[j];
}
}
//printf("\n"); m2 = read();
for (int i = ; i < m2; i++) {
d = read();
int cnt = n;
for (int j = n; j >= ; j--) {
have_train[d][cnt][] = true;
//printf("d:%d cnt:%d\n", d, cnt);
cnt--;
d += ti[j - ];
}
} for (int i = t - ; i >= ; i--) {
for (int j = ; j <= n; j++) {
dp[i][j] = dp[i + ][j] + ;
if (i + ti[j] <= t && have_train[i][j][]) {
dp[i][j] = min(dp[i][j], dp[i + ti[j]][j + ]);
} if (i + ti[j - ] <= t && have_train[i][j][]) {
dp[i][j] = min(dp[i][j], dp[i + ti[j - ]][j - ]);
}
}
} printf("Case Number %d: ", T++);
if (dp[][] >= INF) {
printf("impossible\n");
}
else {
printf("%d\n", dp[][]);
}
}
return ;
}
UVA1025-A Spy in the Metro(动态规划)的更多相关文章
- 【动态规划】[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】动态规划
题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...
- 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站所需的最少等待时间,有 ...
随机推荐
- mybatis_06SQL片段
个人概要: SQL片段在使用if where的基础上,将if,where语句装到SQL标签内,在数据库操作元素内引用 Mybatis提供了SQL片段的功能,可以提高SQL的可重用性. <!--声 ...
- python文件
目录 1. 文件的概念 1.1 文件的概念和作用 1.2 文件的存储方式 2. 文件的基本操作 2.1 操作文件的套路 2.2 操作文件的函数/方法 2.3 read 方法 -- 读取文件 2.4 打 ...
- 课程作业——熟悉常用的Linux操作
cd命令:切换目录 (1) 切换到目录 /usr/local cd /usr/local (2) 去到目前的上层目录 cd .. (3) 回到自己的主文件夹 cd ~ ls命令:查看文件与目录 (4) ...
- WORLD 合并多个WORLD中的文本
1,把多个文档放入一个文件夹中. 2,新建一个WORLD文档. 3,点击插入----对象----文件中的文字----选中文件中的多个WORLD文档----打开.
- K8S RBAC
API Server 内部通过用户认证后,然后进入授权流程.对合法用户进行授权并且随后在用户访问时进行鉴权,是权限管理的重要环节. 在 kubernetes 集群中,各种操作权限是赋予角色(Role ...
- js 格式化数字(每三位加逗号)
// 方法一 unction toThousands(num) { var result = [ ], counter = 0; num = (num || 0).toString().split(' ...
- BDD实战篇 - .NET Core里跑Specflow - 可以跑集成测试和单元测试
这是<如何用ABP框架快速完成项目 >系列中和DevOps系列文章其中一篇文章. BDD很赞!比TDD先进很多,能够大大提高编码效率. 上一篇文章说了如何在.NET Core里安装 ...
- python+selenium+PhantomJS爬取网页动态加载内容
一般我们使用python的第三方库requests及框架scrapy来爬取网上的资源,但是设计javascript渲染的页面却不能抓取,此时,我们使用web自动化测试化工具Selenium+无界面浏览 ...
- "BLAME" is out.
The latest feature animation film "BLAME" is watchable on the Netflix. Rendering was done ...
- SQL Server等待事件—RESOURCE_SEMAPHORE_QUERY_COMPILE
等待事件介绍 关于等待事件RESOURCE_SEMAPHORE_QUERY_COMPILE,官方的介绍如下: Occurs when the number of concurrent query co ...