Problem UVA1025-A Spy in the Metro

Accept: 713  Submit: 6160
Time Limit: 3000 mSec

Problem Description

Input

 Output

For each test case, print a line containing the case number (starting with 1) and an integer representing the total waiting time in the stations for a best schedule, or the word ‘impossible’ in case Maria is unable to make the appointment. Use the format of the sample output.
 

 Sample Input

4
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(动态规划)的更多相关文章

  1. 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍

    参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...

  2. 【Uva1025 A Spy in the Metro】动态规划

    题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...

  3. UVA1025 A Spy in the Metro —— DP

    题目链接: https://vjudge.net/problem/UVA-1025 题解: 详情请看紫书P267. 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写. 递推: #include ...

  4. Uva1025 A Spy in the Metro

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...

  5. 题解:UVa1025 A Spy in the Metro

    原题链接 pdf 题目大意 给出一张无向图图,求该图的最小瓶颈生成树. 无向图的瓶颈生成树:无向图\(G\)的一颗瓶颈生成树是这样的一颗生成树:它最大的边权值在\(G\)的所有生成树中是最小的.瓶颈生 ...

  6. 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 ...

  7. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  8. uva 1025 A Spy in the Metro 解题报告

    A Spy in the Metro Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug Secr ...

  9. uva 1025 A Spy int the Metro

    https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...

随机推荐

  1. OpenLiveWriter.exe已停止工作---解决办法

    一.起因 在win10的系统中成功的安装了OpenLiveWriter,但是在家里win7的电脑上装不上.点击 OpenLiveWriterSetup.exe的安装包后出现 OpenLiveWrite ...

  2. mapper代理方式开发

    使用mapper代理方式开发: 需要编写mapper接口,UserMapper.java需要编写映射文件,UserMapper.xml需要遵循一些开发规范,mybatis便可以自动生成mapper接口 ...

  3. Angular6+ng-zorro实现登录页面

    一.效果图 二.html代码 <div class="login-container"> <div class="login-box"> ...

  4. mac svn的使用

    一.概述 在windows下,我们常常用TortoiseSVN管理svn代码.在mac下,自带svn客户端和服务器端功能. 二.服务端:创建代码仓库,用来存储客户端所上传的代码 (1)创建svn代码存 ...

  5. Python 标准类库-Windows特殊服务之msvcrt

    标准类库-Windows特殊服务之msvcrt   by:授客 QQ:1033553122 广告:出售自研自动化小平台(无需编码也可用),有需要请联系 测试环境 win7 64位 Python 3.4 ...

  6. Android为TV端助力 fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...

  7. Android TextView自动换行、排列错乱问题及解决

    解决之前层次不齐的排版截图,如下图:               解决之后的整齐排版截图,如下图:        今天忽然发现android项目中的文字排版参差不齐的情况非常严重,不得不想办法解决一下 ...

  8. 品牌电脑硬盘损坏后,使用MediaCreationTool从微软官方下载正版Windows到USB做安装盘

    最近我的一台台式机电脑的硬盘损坏了.一开始是速度逐渐变慢,后来慢得难以忍受,有时半天无响应.查看 Windows event ,发现有 id 为 7 的磁盘报错.使用 Windows 8.1 家庭版自 ...

  9. SQL Script

    查某字段是否有汉字 SELECT*FROM TB WHERE COL LIKEN'%[吖-咗]%'

  10. go语言打造个人博客系统(一)

    go语言打造个人博客系统(一) 为什么选择go语言?   听说go语言是在几年前,但真正深入了解他却是在2017年,因为当时作为讲师 ,需要准备go语言的课程,结果稍一接触立刻就喜欢上这门语言了,作为 ...