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. 在同一个机器中安装LoadRunner与QTP

    若你计划在测试机上安装LoadRunner并且测试机上已经安装了QTP,类似这样的情况可能会出现一些冲突现象,若QTP与LR必须并存在同一测试机上,那么请确保先安装Loadrunner以及所有的LR补 ...

  2. 斯坦福大学 iOS 7应用开发 ppt

    上网的找了很久都不全,最后发现原来网易那个视频下面就有完整的PPT..

  3. Android Studio下载与安装

    Android Studio下载与安装 1 2 3 4 5 分步阅读 百度经验:jingyan.baidu.com 自从Google宣布Android Studio将取代Eclipse,正式成为官方集 ...

  4. nvl函数

    NVL(E1, E2)的功能为:如果E1为NULL,则函数返回E2,否则返回E1本身. 但此函数有一定局限,所以就有了NVL2函数. NVL2(E1, E2, E3)的功能为:如果E1为NULL,则函 ...

  5. 立即执行函数与window.onload作用类似

    (function(){ }()); // 立即执行函数 或者用window.onload=function(){}也可以  

  6. Android View.onMeasure方法的理解

    View在屏幕上显示出来要先经过measure(计算)和layout(布局).1.什么时候调用onMeasure方法? 当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方 ...

  7. [iOS翻译]《iOS7 by Tutorials》系列:iOS7的设计精髓(上)

    简介: 本文翻译自<iOS7 by Tutorials>一书的第一章“Designing for iOS 7”,主要从程序员角度介绍了iOS7的新设计理念,堪称神作!本文翻译仅作学习交流之 ...

  8. java从0开始学——数组,一维和多维

    #,在java中,允许数组的长度为0:也就是允许      int[] zeroLenthArray = new int[0]; #,匿名的数组初始化是合法的:     int[] smallPrim ...

  9. JS判断输入是否为整数和数字的正则表达式

    "^\\d+$" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\\d+)|(0+))$" ...

  10. 【原创】基于Memcached 实现用户登录的Demo(附源码)

    一个简单的Memcached在Net中运用的一个demo.主要技术 Dapper+MVC+Memcached+sqlserver, 开发工具为vs2015+Sql 效果图如下: 登录后 解决方案 主要 ...