题意:

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. JDK8 学习

    http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

  2. Ubuntu环境下Nutch1.2 二次开发(添加中文分词)

    前提nutch1.2已部署到eclipse中 详见:http://www.cnblogs.com/cy163/archive/2013/02/19/2916419.html 1 部署IKAnalyze ...

  3. 欧拉工程第73题:Counting fractions in a range

    题目链接:https://projecteuler.net/problem=73 n/d的真分数 ,当d<=12000时 在 1/3 and 1/2 之间的有多少个 public class P ...

  4. Xamarin.Android 入门之:Listview和adapter

    一.引言 不管开发什么软件,列表的使用是必不可少的,而本章我们将学习如何使用Xamarin去实现它,以及如何使用自定义适配器.关于xamarin中listview的基础和适配器可以查看官网https: ...

  5. MVVM 代码记录

      一.XML <Page x:Class="MVVM.MainPage" xmlns="http://schemas.microsoft.com/winfx/20 ...

  6. Hibernate 多表关联

    hibernate中可以一次对多个表进行数据插入,这种插入类似 Hibernate的关联映射关系有:多对一 ---- many-to-one一对多 ---- one-to-many一对一 ---- o ...

  7. GetKeyState和GetAsyncKeyState以及GetKeyboardState函数的用法与区别

    GetKeyState.GetAsyncKeyState.GetKeyboardState函数的区别: 1.BOOL GetKeyboardState( PBYTE lpKeyState );获得所有 ...

  8. SharePoint2010安装文档

    最低配置要求 操作系统 X64 Window Server 2008 R2 Standard 数据库 X64 SQL Server 2008 R2 CPU X64,4核 内存 8G 硬盘 100G 安 ...

  9. 迷宫问题(bfs的应用)

    问题描述: 定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, ...

  10. JVM 问题排查常用工具

    一. jmap // 打印jvm的堆状况,主要是年轻代和老年代信息 jmap -heap <pid> 如: Heap Configuration: MinHeapFreeRatio = M ...