非常有价值的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. Eclipse中遇到main方法不能运行 的情况

    java.lang.UnsupportedClassVersionError: Bad version number in .class file 造成这种过错是ni的支撑Tomcat运行的JDK版本 ...

  2. 遇到can not resolve app 依赖包的问题

    1.第一种解决方式:build->sdkmannger->sdk tool ->安装android support responsitory和google responsitory ...

  3. MongoDB - MongoDB CRUD Operations, Delete Documents

    Delete Methods MongoDB provides the following methods to delete documents of a collection: Method De ...

  4. 在linux上安装完oracle数据库后,如何修改ORACLE_HOSTNAME

    1.修改HOSTS文件,添加node2到本机IP地址的映射: [root@node2home]# gedit /etc/hosts 最后一行为添加的: 127.0.0.1 localhost loca ...

  5. 【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)

    没有参加,但是之后几天打了哦,第三场AK的CF比赛. CF大扫荡计划正在稳步进行. [A]Olympiad 题意: 给\(n\)个人颁奖,要满足: 至少有一个人拿奖. 如果得分为\(x\)的有奖,那么 ...

  6. imperva-代理安装

    首先创建网关上面的监听端口

  7. 【技巧总结】Penetration Test Engineer[2]-Information gathering

    2.信息收集 信息收集是属于前期交互阶段所需要了解的问题. 2.1.前期交互内容 签署授权文件:首要要和受测试方签订授权协议. 划定范围:指定了一个二级域名作为测试目标,那么其他二级域名在测试范围内. ...

  8. 谈谈.NET MVC QMVC高级开发

    自从吾修主页上发布了QMVC1.0,非常感兴趣,用了半月的时间学习,真的感觉收益非浅,在此声明非常感谢吾修大哥的分享! 1.轻快简单,框架就几个类,简单,当然代码少也就运行快!单纯的MVC,使的如果你 ...

  9. asp.net mvc发送邮件

    参考文献: 第一篇:http://www.cnblogs.com/qinpengming/archive/2011/06/08/2075040.html 第二篇:http://www.cnblogs. ...

  10. pl/sql developer 设置oracle的date类型默认显示样式

    oracle里默认的date显示样式: 我的是汉化过的: 进入后,点击工具->首选项->日期/时间设置即可: 设置后在pl/sql developer中查看: 英文版的操作步骤: Tool ...