紫皮书 非原创……

某城市的地铁是线性的有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. ognl中的#、%和$

    多学点,谢谢兄弟 原文地址:ognl中的#.%和$作者:百合 ognl中的#.%和$ #.%和$符号在OGNL表达式中经常出现,而这三种符号也是开发者不容易掌握和理解的部分.在这里笔者简单介绍它们的相 ...

  2. createObjectURL方法 实现本地图片预览

    ie6 可以直接显示本本地路径的图片 如: <img src="file://c:/3.jpg" />  ~~~网上都说ie7就不支持这种文件系统路径的url,但测试 ...

  3. Qt中所有类型之间的转换

    1.char * 与 const char *的转换 char *ch1="hello11";const char *ch2="hello22";ch2 = c ...

  4. 基于Sql Server 2008的分布式数据库的实践(四)

    原文 基于Sql Server 2008的分布式数据库的实践(四) 数据库设计 1.E-R图 2.数据库创建 Win 7 1 create database V3 Win 2003 1 create  ...

  5. 基于visual Studio2013解决算法导论之050强连通分支

     题目 强连通分支 解决代码及点评 // 强连通分支.cpp : 定义控制台应用程序的入口点. // #include<iostream> #define MAX 100 using ...

  6. 基于visual Studio2013解决算法导论之049活动选择问题

     题目 活动选择问题 解决代码及点评 // 活动选择问题.cpp : 定义控制台应用程序的入口点. // #include<iostream> #define N 100 using ...

  7. Install TightVNC Server in RHEL/CentOS and Fedora to Access Remote Desktops

    Virtual Networking Computing (VNC) is a Kind of remote sharing system that makes it possible to take ...

  8. hdu2243之AC自动机+矩阵乘法

    考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. javascript每日一练(十二)——运动框架

    运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...

  10. 一天一个类--ArrayList之一

    今天开始打算将JDK7种的一些类的源码分析一下,笔者认为了解源码就是了解其实现过程,这是非常重要的,而不是简单的记住方法的使用,关键是了解其思想和目的这才是重要的.所以笔者决定首先将从一些容器下手.[ ...