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. 【MAC】安装神器brew

    安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  2. 快速排序 and 拉格朗日插值查找

    private static void QuictSort(int[] zu, int left, int right) { if (left < right) { ; ; ]; while ( ...

  3. hive -f 传递参数

    hive -f 在执行sql脚本文件的时候是可以传递参数的,但是要注意hive版本: 注意:hive在0.9版本之前是不支持-f传递参数的,只有1.0之后才支持次功能. 使用如下: 1.创建sql脚本 ...

  4. 输出映射resultMap

    ①:编写接口方法 /** * 根据id查询用户 * @param id * @return */ public User queryUserById3(Integer id); ②:编写映射文件 1: ...

  5. 学linux,从Ubuntu开始

    1.安装过程出现0x00000000指令引用的0x00000000内存该内存不能为written 如果你安装的是inux系统 需要在设置-->系统--> 处理器--启用PAE支持我的就是这 ...

  6. mybatis 自动生成文件配置

    maven 依赖配置: <!-- sql server --><dependency> <groupId>com.microsoft.sqlserver</g ...

  7. 一句SQL完成动态分级查询

    在最近的活字格项目中使用ActiveReports报表设计器设计一个报表模板时,遇到一个多级分类的难题:需要将某个部门所有销售及下属部门的销售金额汇总,因为下属级别的层次不确定,所以靠拼接子查询的方式 ...

  8. swipe使用及竖屏页面滚动方法

    基于swipe4写了一个移动端的全屏滚动效果  但是图片始终不能自适应屏幕设备大小  这里记录一下 开始的时候要设置  移动端配置 <meta name="viewport" ...

  9. g4e基础篇#5 创建分支和保存代码

    章节目录 前言 1. 基础篇: 为什么要使用版本控制系统 Git 分布式版本控制系统的优势 Git 安装和设置 了解Git存储库(Repo) 起步 1 – 创建分支和保存代码 起步 2 – 了解Git ...

  10. EOS智能合约开发(二):EOS创建和管理钱包

    上节介绍了EOS智能合约开发之EOS环境搭建及启动节点 那么,节点启动后我们要做的第一件事儿是什么呢?就是我们首先要有账号,但是有账号的前提是什么呢?倒不是先创建账号,而是先要有自己的一组私钥,有了私 ...