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站所需的最少等待时间,有 ...
随机推荐
- TCP连接与释放
TCP连接的建立 三次握手 TCP服务器进程先创建传输控制块TCB,时刻准备接受客户进程的连接请求,此时服务器就进入了LISTEN(监听)状态. TCP客户进程也是先创建传输控制块TCB,然后向服务器 ...
- WORLD 文件选择的操作方法
1,按住鼠标左键拖动选择文本. 2,双击鼠标可选中光标前面一个字,如果光标左右两边是一个词,那么就会选中整个词. 3,三击-----整段. 4,光标(鼠标)移至文本左边(外面),变成向右倾斜的光标箭头 ...
- centos7学习笔记-安装配置apache
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 1.安装apache #yum install httpd 2.配置开机启动 systemctl enable httpd 3. ...
- 30.Odoo产品分析 (四) – 工具板块(2) – 搜索和仪表盘(2)
查看Odoo产品分析系列--目录 在前面的模块中,简单介绍过了odoo如何搜索系统中的各种数据集,并保存这些过滤器,以便在之后需要时能够轻松访问这些过滤器.这里将做更详细的介绍.最后分析仪表盘的功能, ...
- 观察者模式与.NET的delegate、event机制
1.引言 最近在写一些程序玩的时候,接触到了delegate(委托)和event(事件),网上查找了很多的资料,有些博文说可以把delegate近似当做C++当中的函数指针来看,由于自己本身对C++的 ...
- iOS ----------要学习的地方(链接整理)
1.http://www.cocoachina.com/special/xcode/ 2.http://blog.csdn.net/a416863220/article/details/4111387 ...
- Android为TV端助力 eclipse出现感叹号的解决办法
当eclipse导入项目出现红叉但无提示错误时,去看:1>菜单路径----Window/Show View/Console2>菜单路径----Window/Show View/Error ...
- mumu模拟器安装xposed--如何在android模拟器上进行root
问题描述 安装xposed表示failed to access root权限,新版的mumu模拟器没有了root选项,需要自己root. 1.先关掉应用兼容性,然后重启 电脑一般都是x86的,mumu ...
- 章节七、3-ArrayList和LinkedList对比
一.创建集合并添加元素(从末尾位置添加) package ZangJie7; import java.util.ArrayList; import java.util.LinkedList; impo ...
- Android平台下利用zxing实现二维码开发
Android平台下利用zxing实现二维码开发 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描库主要有zxing和zbar,zbar在iPos平 ...