描述


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3466

某城市的地铁是线性的,有n个车站,有M1辆列车从左到右开,M2辆列车从右到左开.在0时刻,你在第一站,要在T时刻到达第n站,其间可以随意换车,要求在车站等待的时间最短.

分析


用dp[i][j]表示在i时刻,在第j站,要达到目的,需要等待多久.显然dp[T][n]=0,对于任意i!=n,dp[T][i]=INF,要求dp[0][1].那么转移方程:

dp[i][j]=1.dp[i+1][j]+1(在车站等一分钟).

   2.dp[i+t[j]][j+1](如果有,乘坐向右的列车).

   3.dp[i+t[j-1]][j-1](如果有,乘坐向左的列车).

 #include <bits/stdc++.h>
using namespace std; const int maxn=+,maxt=+,INF=0x3fffffff;
int n,T,M1,M2,kase;
int t[maxn],dp[maxt][maxn];
bool has_train[maxt][maxn][]; void solve(){
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]+;
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-]);
}
printf("Case Number %d: ",++kase);
if(dp[][]>=INF) puts("impossible");
else printf("%d\n",dp[][]);
}
void init(){
scanf("%d",&T);
for(int i=;i<n;i++){
scanf("%d",&t[i]);
}
memset(has_train,,sizeof has_train);
scanf("%d",&M1);
for(int i=;i<=M1;i++){
int d; scanf("%d",&d);
int station=;
while(d<=T&&station<n){
has_train[d][station][]=true;
d+=t[station++];
}
}
scanf("%d",&M2);
for(int i=;i<=M2;i++){
int d; scanf("%d",&d);
int station=n;
while(d<=T&&station>){
has_train[d][station][]=true;
d+=t[station-]; station--;
}
}
}
int main(){
while(scanf("%d",&n)&&n){
init();
solve();
}
return ;
}

UVA_1025_A_Spy_in_the_Metro_(动态规划)的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. 四、使用Maven和使用Eclipse构建javaWeb项目

    环境前边已经搭建过了,我们就再弄了. 1.使用Maven构建javaWeb项目 (1).键入以下命令: $ mvn archetype:generate -DgroupId=com.holytax.w ...

  2. UITextField文本字段控件的位置

    如果需要更改默认的UITextField清除按钮.左右视图等等控件的位置,可以通过如下设置: // 控制清除按钮的位置 (默认 width = 15 = height) -(CGRect)clearB ...

  3. UItextField常用方法

    - (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view ...

  4. ECMA5.1中关于encodeURI,decodeURI 和encodeComponentURI,decodeComponentURI的区别

    The encodeURI and decodeURI functions are intended to work with complete URIs; theyassume that any r ...

  5. svn 日志 offline 错误

    http://zhidao.baidu.com/question/200406861.html (转) #anon-access = read anon-access = none #把read改成n ...

  6. Yii 权限分级式访问控制实现(非RBAC法)

    以下由我们在信易网络公司开发项目的时候终结出的一些经验 主要参考资料:yii官网http://www.yiiframework.com/wiki/60/yii framework 提供了2套权限访问系 ...

  7. 【python】求水仙数

    for i in range(100, 1000): sum = 0 temp = i while temp: sum = sum + (temp%10) ** 3 temp //= 10 # 注意使 ...

  8. 【python】for循环一列

    a="哈哈哈哈,笑死我了"for i in a: print (i, end=' ') 结果 哈 哈 哈 哈 , 笑 死 我 了

  9. Laravel 安装记录

    系统环境:ubuntu-14.10-server-i386(32) LAMP 系统默认安装 根据:http://laravel.com/docs/5.1  官方文档,准备通过 Composer 安装 ...

  10. Spring和Hibernate相遇

    Spring是一个很贪婪的家伙,看到他的长长的jar包列表就知道了,其实对于hibernate的所有配置都是可以放在Spring中来进行得,但是我还是坚持各自分明,Spring只是负责自动探测声明类( ...