非常有价值的dp题目  也是我做的第一题dp    真的效率好高

题意:某城市的地铁是线性的 有n个车站 从左到右编号为1-n  有m1辆列车从第一站开始往右开 还有m2辆列车从第n站开始往左开  在时刻0  小明从第1站出发  目的是在时刻T 正好会见在第n站的间谍  为了不被抓   小明在车站等待的时间要尽量少   求出最短时间  如果到不了  输出impossible

输入第一行为n  第二行为T  第三行有n-1个数字  表示 从左到右两个车站之间列车行驶的时间   接下来为m1    然后跟着m1个数字 表示车站1发车时刻   然后m2 同理

一开始根本想不到用dp   这题的元素好多 !!

每次有三种决策

1 原地等待1s

2 搭乘往右的车  (当然 前提是有)

3 搭乘往左的车

当面临多决策问题时  且所处环境(时间 地点)多样时   用dp做!!!

影响决策的元素只有两个  1 时间  2 所处车站

所以 dp i j     i表示当前时刻   j 表示所处车站

从结束点  也就是 时刻为T时开始进行dp

注意对dp数组的初始化   都在i=T的情况下   当i==n时   达成目标   dp为0     当i为其他值时  全部为inf(显然已经没机会了)

非常好的dp  !!

#include<bits/stdc++.h>
using namespace std;
#define N 200+5
#define inf 0x3f3f3f3f
int n,t[N],car[N][N][];
int dp[N][N];
int main()
{
int cas=;
int T;
int q;
int x;
while(scanf("%d",&n)==,n)
{ memset(car,,sizeof car);
scanf("%d",&T);
for(int i=;i<n-;i++)scanf("%d",&t[i]); scanf("%d",&q);
while(q--)
{
scanf("%d",&x);
car[][x][]=;
for(int i=;i<n;i++)
{
x+=t[i-];
car[i][ x ][]=;
}
}
scanf("%d",&q);
while(q--)
{
scanf("%d",&x);
car[n-][x][]=;
for(int i=n-;i>=;i--)
{
x+=t[i];
car[i][x][]=;
}
} 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( car[j][i][] && i+t[j]<=T && j<n- )
dp[i][j]=min(dp[i][j],dp[ i+t[j] ][j+] ); if( car[j][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 ;
}

9-1 A Spy in the Metro uva1025 城市里的间谍 (DP)的更多相关文章

  1. UVa-1025城市里的间谍 A Spy in the Metro

    原题 城市里的间谍 分析 动态规划,dp[i][j]表示你在时刻i,车站j,最少还要等待的时间. 边界条件d[T][n]=0 已经到达,其他d[T][i]=inf不可达. 在一个站点时,有以下三种决策 ...

  2. UVA1025 城市里的间谍

    #include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...

  3. 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍

    参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...

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

  5. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

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

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

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

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

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

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

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

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

随机推荐

  1. 让你快速学会Shell脚本

    Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合.Shell可以直接使用在win/Unix/Linux上面, ...

  2. fopen()、fwrite()、fread()函数使用说明与示例

    fopen()函数: 1.作用: 在C语言中fopen()函数用于打开指定路径的文件,获取指向该文件的指针. 2.函数原型: FILE * fopen(const char * path,const  ...

  3. Shell记录-Shell命令(其他)

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. .命令格式 top [参数] Shell 2.命令功能 显示当前系统正在执行的 ...

  4. Python之路,Day2 - Python基础,列表,循环

    1.列表练习name0 = 'wuchao'name1 = 'jinxin'name2 = 'xiaohu'name3 = 'sanpang'name4 = 'ligang' names = &quo ...

  5. bzoj千题计划170:bzoj1968: [Ahoi2005]COMMON 约数研究

    http://www.lydsy.com/JudgeOnline/problem.php?id=1968 换个角度 一个数可以成为几个数的约数 #include<cstdio> #incl ...

  6. 哈密顿图 哈密顿回路 哈密顿通路(Hamilton)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5452580.html 概念: 哈密顿图:图G的一个回路,若它通过图的每一个节点一次,且仅一次,就是哈密顿回路.存在哈密顿回 ...

  7. Bzoj4763 雪辉

    Time Limit: 39 Sec  Memory Limit: 666 MBSubmit: 151  Solved: 80 Description 上次立下的NOIP退役Flag没有成功   这次 ...

  8. 原生的js实现jsonp的跨域封装

    一.原理 jsonp是利用浏览器请求script文件时不受同源策略的限制而实现的,伪造一个script标签,将请求数据的url赋值给script的src属性,并将该标签添加到html中,浏览器会自动发 ...

  9. Python time()方法

    from:http://www.runoob.com/python/att-time-time.html 描述 Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数 ...

  10. Linux Kernel sys_call_table、Kernel Symbols Export Table Generation Principle、Difference Between System Calls Entrance In 32bit、64bit Linux【转】

    转自:http://www.cnblogs.com/LittleHann/p/4127096.html 目录 1. sys_call_table:系统调用表 2. 内核符号导出表:Kernel-Sym ...