第一遍,刘汝佳提示+题解;回头再看!!!

POINT:

  dp[time][sta];  在time时刻在车站sta还需要最少等待多长时间;

  终点的状态很确定必然是的 dp[T][N] = 0 ---即在T时刻的时候正好达到N站点

  我们可以 从终点的状态往起始的状态转化, 一步步走就可以了。

  has_train[t][i][0];  t时刻在i车站是否有往右开的火车

  has_train[t][i][1];  t时刻在i车站是否有往左开的火车

 #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
using namespace std; #define INF 0xffffff7
#define maxn 200+10 int N, T;
int t[maxn];
int dp[maxn][maxn];//dp[i][j] i时刻在j车站最少等候时间
int has_train[maxn][maxn][];
int main()
{
//freopen("out.txt", "r", stdout);
int kase = ;
int N;
while(scanf("%d", &N) && N)
{
memset(has_train, , sizeof(has_train));
memset(t, , sizeof(t));
kase++;
scanf("%d", &T);
for(int i = ; i < N; i++)
scanf("%d", &t[i]); int M1, M2;
scanf("%d", &M1);
for(int i = ; i <= M1; i++)
{
int start;
scanf("%d", &start);
has_train[start][][] = ;  // 标记初始站台
for(int j = ; j < N; j++)
{
int time = start + t[j];
if(time <= T)
{
has_train[time][j+][] = ;
start += t[j];
}
}
}
scanf("%d", &M2);
for(int i = ; i <= M2; i++)
{
int start;
scanf("%d", &start);
has_train[start][N][] = ;  // 标记初始站台
for(int j = N-; j >= ; j--)
{
int time = start + t[j];
if(time <= T)
{
has_train[time][j][] = ;
//cout << "has[" << time << "][" << j << "][1] " << endl;
start += t[j];
}
}
}
printf("Case Number %d: ", kase); for(int i = ; i < N; i++)
dp[T][i] = INF;
dp[T][N] = ;
for(int i = T-; i >= ; i--)
{
for(int j = ; j <= N; j++)
{
dp[i][j] = dp[i+][j] + ; //第T-1时刻在j车站最少等候时间 = 第T时刻在j车站最少等候时间+1; 即决策一---等一分钟
if(j < N && has_train[i][j][] && i + t[j] <= T) //决策二
{
dp[i][j] = min(dp[i][j], dp[i + t[j]][j+]);
}
if(j > && has_train[i][j][] && i + t[j-] <= T) //决策三
{
dp[i][j] = min(dp[i][j], dp[i + t[j-]][j-]);
}
}
} if(dp[][] >= INF) printf("impossible\n");
else printf("%d\n", dp[][]);
}
return ;
}

DAG的动态规划 (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 " (DAG上的动态规划?? or 背包问题??)

    传送门 参考资料: [1]:算法竞赛入门经典:第九章 DAG上的动态规划 题意: Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过: 其中 M1 辆列车从 1 ...

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

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

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

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

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

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

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

  9. uva 1025 A Spy int the Metro

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

随机推荐

  1. 使用Cygwin通过ssh命令行来访问Windows8

    安装Cygwin可以参考<如何在Windows中通过Cygwin来使用Linux命令>. 在Win8下貌似有个bug,需要将cygwin\bin\mintty 修改为cygwin\bin\ ...

  2. 您需要来自administrators的权限才能对此文件进行更改

    今天我重装了系统,以前D盘里的一个文件夹想删除,可以一直没法删除,原先它提示"您需要来自 S-1-5-21-602162358-1284227242-682003330-500 的权限才能对 ...

  3. HDU 5805 NanoApe Loves Sequence (模拟)

    NanoApe Loves Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5805 Description NanoApe, the ...

  4. HDU 5832 A water problem (带坑水题)

    A water problem 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

  5. java assert的用法简介

    assert的基本用法 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...

  6. ASP.NET Web Form和MVC中防止F5刷新引起的重复提交问题

    转载 http://www.cnblogs.com/hiteddy/archive/2012/03/29/Prevent_Resubmit_When_Refresh_Reload_In_ASP_NET ...

  7. css知识汇总

    <style type="text/css"> table{ border-collapse:collapse; } table, td, th{ border:1px ...

  8. java commons-lang 工具包 逃脱工具 转unicode 及其他

    <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</ar ...

  9. 介绍50个 WordPress 动作挂钩

    WordPress 之所以能成为世界上最受欢迎的网页内容管理系统,原因就在于它的高度灵活性和可塑性,而这种灵活性和可塑性正是由“挂钩”(Hooks)简洁宜用的结构所决定的.可以说,没有过滤挂钩(Fil ...

  10. python的sys.path

    python检测不到模块: No module named 是因为模块没有在sys.path中,查看sys.path的方法 import sys sys.path 发现确实没有加载到模块. windo ...