紫皮书 非原创……

某城市的地铁是线性的有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. sobel流水线操作Verilog程序

    sobel算子的verilog实现,采用了流水线操作 module sobel_computer ( clock , reset, OrigDataEn, //SobelAluEn, OrigData ...

  2. 制作Linux(Fedora、Ubuntu、CentOS)优盘启动

    随着嵌入式技术的快速发展,Linux快速发展过一段时间.虽然现在不是很热,但是linux在现实社会的使用还是很有用处.而光盘有有些落伍,不仅浪费而且不环保,所以质优价廉的优盘就脱颖而出.所以,用优盘制 ...

  3. 北京创客空间 BEIJING MAXPACE的小站

    北京创客空间 BEIJING MAXPACE的小站 北京市海淀区海淀大街1号中关村梦想实验室(原中关村国际数字设计中心)4层

  4. 项目总结SpringMVC+hibernate框架 web.xml 分析(2)

    紧接 项目总结SpringMVC+hibernate框架 原理(MVC) applicationContext.xml 文件(3) 这一步讲解项目模块化的配置,项目中每个模块配置一个文件,命名规则为 ...

  5. Android Dialog详解

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  6. 自己设计的SSO登录流程图

    这个图上不考虑安全加密.由于本身SSO流程图已经比較复杂了.可能还有问题,欢迎大家拍砖. 1.登录流程图: 2.退出流程图: 3.改进方面: 每一个应用登录后.直接将ticket写入session中, ...

  7. 利用ant的javac任务来编译程序使用ant的java任务来运行程序

    <?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...

  8. java--局部类只能访问外包方法的final局部成员

    class B523{ // private int k = 10; public void go(int x, final int y){ // int a = x+y; final int b = ...

  9. QLinkedList和std::forward_list(都是双向链表,不支持operator[],好处可能是插入和删除都比较快)

    forward_list forward_list是C++11版本才有的.forward_list被实现为单链表,而list是一个双向链表,所以forward_list要比list高效一些.forwa ...

  10. 基于二叉树和数组实现限制长度的最优Huffman编码

    具体介绍详见上篇博客:基于二叉树和双向链表实现限制长度的最优Huffman编码 基于数组和基于链表的实现方式在效率上有明显区别: 编码256个符号,符号权重为1...256,限制长度为16,循环编码1 ...