紫皮书 非原创……

某城市的地铁是线性的有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. 初识Channel

    java.nio.channels 中的接口和类. A channel represents an open connection to an entity such as a hardware de ...

  2. django中tag的用法

    在app里建一个子的python包,包含__init__.py,包名为templatetags,里面新建一个tags.py(这个名字可以随意) from django import templater ...

  3. 用Verilog实现IIC通讯

    注意,此代码是错误代码,并不能实现想要的结果. 之所以留着,因为里面的enable 是独立开来的思想值得借鉴.就是控制单元和运算单元分开(我也是借鉴别人的实现思想).具体用verilogHDL实现II ...

  4. Android 代码设置密码输入框内容的显示/隐藏

    //内容可见 mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //内容不可见 m ...

  5. ASP.NET MVC 5 学习教程:添加验证

    原文 ASP.NET MVC 5 学习教程:添加验证 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控 ...

  6. C++变量(C++变量定义、变量赋值、命名规则)

    其实在前面的例子中已经多次用到了变量.在程序运行期间其值可以改变的量称为变量.一个变量应该有一个名字,并在内存中占据一定的存储单元,在该存储单元中存放变量的值.请注意区分变量名和变量值这两个不同的概念 ...

  7. javascript每日一练(六)——事件一

    一.event对象 var oEvent = ev || event;//获取事件对象 oEvent.clientX oEvent.clientY//获取鼠标坐标 oEvent.cancelBubbl ...

  8. 02-Foundation-NSMutableString、NSNumber、NSValue、NSDate、NSArray

    目录: 一.NSMutableString可变字符串 二.NSNumber数字对象 三.NSValue 四.NSDate日期对象 五.NSArray数组对象 回到顶部 一.NSMutableStrin ...

  9. android在view.requestFocus(0)返回false的解决办法

    我们有时候想让listview的第一行自动获取到焦点,我们就会使用view.requestFocus(0)来操作,而有时候并不生效,debug后显示rerurn为false. 这是因为我们获取焦点太早 ...

  10. android之wifi开发

    WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为“热点 ...