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

也就是尽量多坐车,最后输出最少等待时间。

析:这个挺复杂,首先时间是一个顺序,设d(i,j)表示时刻 i 在第 j 个车站,最少还要等待多长时间,那么边界是d(T, n) = 0。

并且有三种决策:

决策一:等着 d[i][j] = d[i + 1][j] + 1; 为什么从i + 1 过来呢? 你想一下,DP表示等待的时间,那么是不是应该倒着来呢?

决策二:有往右行驶的车  d[i][j] = min(d[i][j],  d[i + t[j]][j];

决策三:有往左行驶的车  d[i][j] = min(d[i][j], d[i + t[j - 1]][j]);

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int INF = 0x3f3f3f3f; int main(){
int T, n, t[80], dp[205][60], m1, m2, train[255][55][2], d, k = 0; while(scanf("%d", &n) && n){
scanf("%d", &T);
t[0] = 0;
for(int i = 1; i < n; ++i)
scanf("%d", &t[i]);
scanf("%d", &m1);
memset(train, false, sizeof(train));
for(int i = 0; i < m1; ++i){
scanf("%d", &d);
// train[d][1][0] = true;
int s = d;
for(int j = 0; j < n; ++j){
s += t[j];
if(s <= T) train[s][j+1][0] = true;
else break;
}
} scanf("%d", &m2);
for(int i = 0; i < m2; ++i){
scanf("%d", &d);
train[d][n][1] = true;
int s = d;
for(int j = n-1; j > 1; --j){
s += t[j];
if(s <= T) train[s][j][1] = true;
else break;
}
} for(int i = 1; i < n; ++i) dp[T][i] = INF;
dp[T][n] = 0; for(int i = T-1; i >= 0; --i) /// t
for(int j = 1; j <= n; ++j){ /// g
dp[i][j] = dp[i+1][j] + 1;
if(j < n && train[i][j][0] && i + t[j] <= T)
dp[i][j] = min(dp[i][j], dp[i+t[j]][j+1]);
if(j > 1 && train[i][j][1] && i + t[j-1] <= T)
dp[i][j] = min(dp[i][j], dp[i+t[j-1]][j-1]);
} if(dp[0][1] >= INF) printf("Case Number %d: impossible\n", ++k);
else printf("Case Number %d: %d\n", ++k, dp[0][1]);
}
return 0;
}

UVa 1025 A Spy in the Metro (DP动态规划)的更多相关文章

  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(动态规划)

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

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

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

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

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

  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. DAG的动态规划 (UVA 1025 A Spy in the Metro)

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

  8. uva 1025 A Spy int the Metro

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

  9. UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)

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

随机推荐

  1. socket编程---SCTP

    sctp_sndrcvinfo结构体 sctp_event_subscribe结构体 更多的关于SCTP的结构体http://aisxyz.iteye.com/blog/2408978 SCTP套接字 ...

  2. 【linux】less &&more

    命令 :         less [文件名]                                                                              ...

  3. Django Admin定制

    Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django.contrib.contenttyp ...

  4. Makefile中进行宏定义-***

    实际上是gcc命令支持-D宏定义,相当于C中的全局#define: gcc -D name gcc -D name=definition Makefile中可以定义变量(和宏很像),但是是给make解 ...

  5. hudson插件说明

    Artifactory Plugin:maven仓库管理工具 Backup plugin 可以备份hudson_home下所有文件,除了svncode.这个插件有问题,不能使用. Build Publ ...

  6. (转)Flex开发工具Flex Builder 3 下载及注册码

    本文转载自:http://blog.csdn.net/wlxtaking/article/details/5779762 Flex是通过java或者.net等非Flash途径,解释.mxml文件组织c ...

  7. configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

    编译php出现错误: configure: error: Please reinstall the libcurl distribution - easy.h should be in <cur ...

  8. Javascript继承机制的设计思想

    转自:http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.htm ...

  9. sort+函数指针、sort+比较器对象、qsort速度比较

    一.上代码 #include<bits/stdc++.h> using namespace std; #define MAXN 50000000 struct TS { int a, b, ...

  10. word2vec相关

    word '\xe8\xb6\x85\xe8\x87\xaa\xe7\x84\xb6\xe7\x8e\xb0\xe8\xb1\xa1' not in vocabulary 分词后的样本格式:英雄联盟, ...