题意:

n个车站,已知到达相邻车站的时间,有m1辆车从1站出发已知发车时间,有m2辆车从n站出发已知发车时间,求从1到达n所需等车的总时间最小。

分析:

有三种情况,在原地等,乘左到右的车,乘右到左的车dp[i][j]=min(dp[i][j+1]+1,dp[i-1][j+t[i-1]],dp[i+1][t[i]]);

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int dp[][],ld[][],rd[][];
int n,T,m,t[];
void solve(){
memset(dp,0x3f,sizeof(dp));
dp[n][T]=;
for(int j=T-;j>=;--j)
for(int i=;i<=n;++i)
{
dp[i][j]=dp[i][j+]+;
if(ld[i][j])dp[i][j]=min(dp[i][j],dp[i-][j+t[i-]]);
if(rd[i][j])dp[i][j]=min(dp[i][j],dp[i+][j+t[i]]);
}
if(dp[][]>T)printf("impossible\n");
else printf("%d\n",dp[][]);
}
int main()
{
int ca=;
while(~scanf("%d",&n)){
if(n==)break;
scanf("%d",&T);
for(int i=;i<n;++i)
scanf("%d",&t[i]);
int tmp;
memset(ld,,sizeof(ld));
memset(rd,,sizeof(rd));
scanf("%d",&m);
while(m--){
scanf("%d",&tmp);
for(int i=;i<=n;++i)
{
rd[i][tmp]=;
tmp+=t[i];
}
}
scanf("%d",&m);
while(m--){
scanf("%d",&tmp);
for(int i=n;i>;--i)
{
ld[i][tmp]=;
tmp+=t[i-];
}
}
printf("Case Number %d: ",++ca);
solve();
}
return ;
}

A Spy in the Metro的更多相关文章

  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. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  3. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

  4. uva 1025 A Spy in the Metro 解题报告

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

  5. uva A Spy in the Metro(洛谷 P2583 地铁间谍)

    A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangero ...

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

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

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

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

  8. UVA1025---A Spy in the Metro(DP)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 Secret agent Maria was sent to Alg ...

  9. 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 ...

  10. uva 1025 A Spy int the Metro

    https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...

随机推荐

  1. lintcode:插入区间

    题目: 插入区间 给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [ ...

  2. 【PHPsocket编程专题(实战篇②)】兼容 Curl/Socket/Stream 的 HTTP 操作类[转]

    <?php /************************************************************ * 描述:HTTP操作类 * 作者:heiyeluren ...

  3. Java学习笔记之:Java封装

    一.引言 在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定 ...

  4. 利用python 获取 windows 组策略

    工作中有时候会有这种需求: 1. 自动配置组策略的安全基线,这个东西不用你自己写了,微软有这个工具,Microsoft Security Compliance Manager,你可以在下面的地址去下载 ...

  5. Android listview的item设定高度

    在item的layout文件中,用android:layout_height设置item的高度.运行,高度设置无效. 解决办法: 给item设定minHeight,即可. -------------- ...

  6. OpenCV源码阅读(3)---matx.h---学习心得

    在.h文件里定义类,可以通过内联函数的方法完成类基础函数的实现,这样就不需要额外写.cpp文件来写类的内容. 对于操作符重载,可以使用返回应用的方式减小内存开销 _Tp& someclass: ...

  7. C++ 打印输出指针

    大家想必在很多场景下需要打印输出指针地址.看一下下面的输出:       CObject* pObject = new CObject;     std::cout << pObject  ...

  8. php整理(二): 数组

    数组: 首先说一下对PHP中的理解,建立一个好的理解模型还是很关键的: 1.PHP中的数组实际上可以理解为键值对,key=>value;而对于key的取值,可以是string/integer;v ...

  9. Android开发之onActivityResult()中的resultCode为0,intent为null的解决办法

    BUG:昨天在使用activity之间传值的时候,遇到了一个bug,该bug为:Activity A启动Activity B,然后在Activity B中取到一个值,并通过back键返回到Activi ...

  10. 10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

    The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk ...