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. Flask 系列之 LoginManager

    说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过使用 flask-login 进行会话管理的相关操作 ...

  2. 如何处理Express异常?

    译者按:根据墨菲定律:“有可能出错的事情,就会出错”.那么,既然代码必然会出错,我们就应该处理好异常. 原文: How to handle errors in Express 译者:Fundebug ...

  3. php生成xml数据

    1.php生成xml数据一般有2种方式, 一个是组装字符串,另一个就是使用php内置的系统类 2.使用php内置类生成xml数据 3.拼装字符串生成xml数据 public function stat ...

  4. [HTML/CSS]创建新元素的三种方法

    第一种:通过text/HTML var txt1="<h1>Text.</h1>"; 第二种:通过jQuery var txt2=$("<h ...

  5. JS单体内置对象之Math常用方法(min,max,ceil,floor,round,random等)

    1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ...

  6. 洛谷P3388 【模板】割点(割顶)(tarjan求割点)

    题目背景 割点 题目描述 给出一个n个点,m条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,m 下面m行每行输入x,y表示x到y有一条边 输出格式: 第一行输出割点个数 第二行按照 ...

  7. 【读书笔记】iOS-更新项目前要注意的事情

    在进行永久更改项目的任何现代化操作之前,要问自己几个问题. 1,我还需要返回项目的旧代码吗? 2,我的同事中有没有人无法升级到最新版本的Xcode? 3,  如果我使用了最新的功能,会不会减少用户? ...

  8. mybatis 自动生成文件配置

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

  9. python地理处理包——Shapely介绍及用户手册

    本文主要是基于shapely官方文档翻译而成 shapely主要是在笛卡尔平面对几何对象进行操作和分析. 性能 Shapely中所有的操作都是使用GEOS库.GEOS是用C++写的,也被用在许多应用程 ...

  10. PyCharm快捷键使用