http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913

预处理出每个时间、每个车站是否有火车

为了方便判断是否可行,倒推处理,每次有三种决策:原地坐等一分钟、搭车向左(如果有车)、搭车向右(如果有车)

 /**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
int n,T;
bool h[mxn][mxn][];
int ti[mxn];
int m,d,e;
int f[mxn][mxn];
void clear(){
memset(ti,,sizeof(ti));
memset(h,,sizeof(h));
memset(f,,sizeof(f));
}
int main(){
int cnt=;
while(scanf("%d",&n) && n){
clear();
scanf("%d",&T);
int i,j;
for(i=;i<n;i++)scanf("%d",&ti[i]);//车站距离
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d",&d);//左边发车时间
for(j=;d<=T && j<=n;d+=ti[j],j++){
h[d][j][]=;
}
}
scanf("%d",&m);
for(i=;i<=m;i++)
{
scanf("%d",&e);//右边发车时间
for(j=n;e<=T && j>=;j--,e+=ti[j]){
h[e][j][]=;
}
}
//以上全是初始化
f[T][n]=;
for(i=T-;i>=;i--){//倒推
for(j=;j<=n;j++){
f[i][j]=f[i+][j]+;//等待
if(j<n && h[i][j][] && i+ti[j]<=T)
f[i][j]=min(f[i][j],f[i+ti[j]][j+]);//向右
if(j> && h[i][j][] && i+ti[j-]<=T)
f[i][j]=min(f[i][j],f[i+ti[j-]][j-]);//向左
}
}
printf("Case Number %d: ",++cnt);
if(f[][]>=0xff) printf("impossible\n");
else printf("%d\n",f[][]);
}
return ;
}

UVa 1025 A Spy in the Metro的更多相关文章

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

  2. UVA 1025 -- A Spy in the Metro (DP)

     UVA 1025 -- A Spy in the Metro  题意:  一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...

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

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

  4. UVa 1025 A Spy in the Metro(动态规划)

    传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...

  5. UVA 1025 A Spy in the Metro 【DAG上DP/逆推/三维标记数组+二维状态数组】

    Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After s ...

  6. uva 1025 A Spy int the Metro

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

  7. DAG的动态规划 (UVA 1025 A Spy in the Metro)

    第一遍,刘汝佳提示+题解:回头再看!!! POINT: dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间: 终点的状态很确定必然是的 dp[T][N] = 0 ---即在 ...

  8. World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)

    分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代 ...

  9. UVa 1025 A Spy in the Metro (DP动态规划)

    题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...

随机推荐

  1. salt-ssh

    1.生产---测试--开发 salt-runners salt-run manage.status#显示当前up或down的minion salt-run manage.down salt-run m ...

  2. matlab批量合并txt文件

    1: %% merge.m 2: %%%%Main程序%%%%%% 3: %%%%%% 4: %%%%%%本程序合并完各个子文件夹中的txt到主文件目录下,并且合并的文件以子文件夹名字命名 5: %% ...

  3. [CareerCup] 3.4 Towers of Hanoi 汉诺塔

    3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes ...

  4. 第十章 系统级I/O

    第十章 系统级I/O 一.Unix I/O 1.一个unix文件就是一个m个字节的序列 2.unix外壳创建的每个进程开始时都有三个打开的文件:标准输入(0) .标准输出(1)和标准错误(-1). 二 ...

  5. RocEDU.阅读.写作《你的灯亮着吗?》

    <你的灯亮着吗?> 一.对本书的认识 这本书的作者就如何训练思维能力指点迷津.书中提及的观点包括"问题是理想状态和现实状态之间的差别",以及"无论表面上表现的 ...

  6. Opencv step by step - 鼠标事件

    鼠标事件有下面几种(没有滚轮事件,比较遗憾): #define CV_EVENT_MOUSEMOVE 0 滑动 #define CV_EVENT_LBUTTONDOWN 1 左键点击 #define ...

  7. .NET Web开发初学者必知的四个网站

    No.1 W3school 链接: http://www.w3school.com.cn/ 预览: 介绍: 全球最大Web前端技术教程网站.内容涵盖从基础的 HTML 到 CSS到进阶的XML.SQL ...

  8. .NET MVC框架中控制器接收参数的四种方式

    1.通过路由中的配置的参数名字直接接收(要求:两者同名) routes.MapRoute(                 name: "Default",             ...

  9. webSocket实现web及时聊天的例子

    概述 websocket目前虽然无法普及应用,未来是什么样子,我们不得而知,但现在开始学习应用它,只有好处没有坏处,本随笔的WebSocket是版本13(RFC6455)协议的实现,也是目前webso ...

  10. 一次非常有意思的sql优化经历

    补充:看到这么多朋友对sql优化感兴趣,我又重新补充了下文章的内容,将更多关于sql优化的知识分享出来, 喜欢这篇文章的朋友给个赞吧,哈哈,欢迎交流,共同进步. 2015-4-30补充:非常感觉编辑的 ...