紫皮书 非原创……

某城市的地铁是线性的有n个车站从左到右编号为1-n,有M1辆地铁从第一站出发,有M2辆车从最后一站出发,mario从第一站出发,目的是在时刻T会见车站n的一个朋友(间谍)。在车站等车容易被抓,所以尽量让其在车站的时间尽量短,mario能完成方向不同地铁的换乘

dp[i][j]表示i时刻j站最少还要等多长时间,边界dp[T][n] = 0;其他dp[T][i] 为正无穷

则有三种决策

1.等一分钟

2.搭乘左开的车

3.搭乘右开的车

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <queue>
#include <map>
#include <set> using namespace std; const int INF = 0xffffff;
const double ESP = 10e-;
const double Pi = * atan(1.0);
const int MAXN = + ;
const long long mod = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,};
typedef long long LL; LL gac(LL a,LL b){
return b?gac(b,a%b):a;
} int dp[][MAXN];
int n,T;
int t[MAXN];
int M1,M2;
int d[MAXN];
int e[MAXN];
bool has_train[][MAXN][]; int main(){
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
// freopen("output.txt","w",stdout);
#endif
int cas = ;
while(~scanf("%d",&n) && n){
scanf("%d",&T);
for(int i = ;i <= n;i++){
scanf("%d",&t[i]);
}
t[] = ;
t[n+] = ;
memset(has_train,,sizeof(has_train));
scanf("%d",&M1);
for(int i = ;i <= M1;i++){
scanf("%d",&d[i]);
int sum = d[i];
for(int j = ;j <= n;j++){
sum += t[j];
has_train[sum][j][] = ;
}
}
scanf("%d",&M2);
for(int i = ;i <= M2;i++){
scanf("%d",&e[i]);
int sum = e[i];
for(int j = n;j > ;j--){
sum += t[j+];
has_train[sum][j][] = ;
}
}
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: ",cas++);
if(dp[][] >= INF){
printf("impossible\n");
}
else{
printf("%d\n",dp[][]);
}
}
return ;
}

uva 1025的更多相关文章

  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 解题报告

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

  3. UVA 1025 -- A Spy in the Metro (DP)

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

  4. uva 1025,城市的间谍

    题目链接:https://uva.onlinejudge.org/external/10/1025.pdf 题意: 地铁是线性的,有n个站,编号(1~n),M1辆从左至右的车,和M2辆从右至左的车,发 ...

  5. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

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

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

  7. UVa 1025 A Spy in the Metro

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 预处理出每个时间.每个车站是否有火车 为了方便判断是否可行,倒推处理 ...

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

    题意: 有线性的n个车站,从左到右编号分别为1~n.有M1辆车从第一站开始向右开,有M2辆车从第二站开始向左开.在0时刻主人公从第1站出发,要在T时刻回见车站n 的一个间谍(忽略主人公的换乘时间).输 ...

  9. DAG的动态规划 (UVA 1025 A Spy in the Metro)

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

随机推荐

  1. CodeForces 189A 166E 【DP ·水】

    非常感谢 Potaty 大大的援助使得我最后A出了这两题DP ================================== 189A : 求切分后的ribbon最多的数目,不过要求切分后只能存 ...

  2. 让Solr返回JSON数据

    http://localhost:1985/solr/select/?q=*%3A*&version=2.2&start=0&rows=10&indent=on& ...

  3. linux下java窗口,正确显示中文

    Tip1 1.在 JAVA_HOME/jre/lib/fonts/ 下建立个目录 fallback 2.在 fallback 里弄个中文字体最简单ln一下就好了 比如: ln -s /usr/shar ...

  4. WeCenter 社交化问答社区程序 | WeCenter 是一款知识型的社交化问答社区程序,专注于社区内容的整理、归类、检索和再发行

    WeCenter 社交化问答社区程序 | WeCenter 是一款知识型的社交化问答社区程序,专注于社区内容的整理.归类.检索和再发行 为什么选择 WeCenter 程序? 让您的社区更智能地运作,强 ...

  5. Codeforces 331A2 - Oh Sweet Beaverette (70 points)

    贪心搞就行,用map记录每个数出现的下标,每次都取首尾两个.将中间权值为负的删掉后取sum值最大的就行. #include<iostream> #include<algorithm& ...

  6. ORM框架Hibernate (四) 一对一单向、双向关联映射

    简介 在上一篇博客说了一下多对一映射,这里再说一下一对一关联映射,这种例子在生活中很常见,比如一个人的信息和他的身份证是一对一.又如一夫一妻制等等. 记得在Java编程思想上第一句话是“一切皆对象”, ...

  7. Struct初学的,页面跳转

    Filter控制器 jsp页面代码 <form action="page_login.action" method="post">     user ...

  8. 网页制作之JavaScript部分 2 - DOM操作

    1.DOM的基本概念  htmlDOM是一种面向对象的树的模型,它包含html中的所有元素:通过html可以找到所有包含在dom中的元素. DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对 ...

  9. 基于visual Studio2013解决算法导论之011快排改良

     题目 快排改良 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...

  10. android开发之蓝牙配对连接的方法

    最近在做蓝牙开锁的小项目,手机去连接单片机总是出现问题,和手机的连接也不稳定,看了不少蓝牙方面的文档,做了个关于蓝牙连接的小结. 在做android蓝牙串口连接的时候一般会使用 ? 1 2 3 4 5 ...