题意:一个间谍要从第一个车站到第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. c++交叉#include问题

    这个问题会引起莫名其妙的编译错误, 碰到类里有其他类的指针的话,不要#include,提前声明下即可 class_a.h #ifndef CLASS_A_H #define CLASS_A_H cla ...

  2. Http消息头中常用的请求头和响应头

    作为Web开发对常用http的请求头和响应头熟悉了解一下还是很有必要的.比如请求头中Content-type指定了请求的内容,若类型是application/x-www-form-urlencoded ...

  3. python连接redis sentinel集群

    安装 python redis 客户端 pip install redis #!/usr/bin/env python # -*- coding:utf-8 -*- #!/usr/bin/env py ...

  4. servlet深探

    在spring4下面使用的是ServletContext作为容器,这个是servlet规范里面设置的:加载了默认的servlet(在spring 4之前都是web.xml中做的),但是在spring ...

  5. Phonegap 工程项目介绍

    一.工程项目的路径在www下面,www下面的文件如下图 1. index.html <!DOCTYPE html> <!-- Licensed to the Apache Softw ...

  6. JAVA通过JDBC连接Oracle数据库详解【转载】

    JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...

  7. wheezy下安装emacs24

    wget -q -O - http://emacs.naquadah.org/key.gpg | sudo apt-key add - vim /etc/apt/sources.list 添加 deb ...

  8. application/json 和 application/x-www-form-urlencoded的区别

    public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateVali ...

  9. django-url命名空间+反查

    from django.conf.urls import url, include urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^h ...

  10. Cordova+Vue构建Hybrid APP简易实操

    当下APP市场,因为Native APP开发成本高,Web APP不稳定,混合开发APP大行其道,成为越来越多开发者的首选.Hybrid APP开发框架也比较多,Weex.Ionic.PhoneGap ...