分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代价为1。第二种:如果可以则向右上车,转移到dp[i+t][j+1],无代价,t为列车行驶时间。第三种与第二种相同。初始状态为dp[0][1] = 0,其他为INF。答案为dp[T][n]。

  代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define N 55
#define INF 1e9
bool gol[N][N*],gor[N][N*];
int dp[N*][N];
int main()
{
int n,T,t[N],m1,m2,d,e,sum,ans,ca=;
while(cin>>n && n)
{
cin>>T;
for(int i = ; i <= n; i++)
{
cin>>t[i];
}
memset(gol,false,sizeof(gol));
memset(gor,false,sizeof(gor));
cin>>m1;
for(int i = ; i < m1; i++)
{
cin>>d;
gor[][d] = true;
sum = d;
for(int j = ; j <= n; j++)
{
sum += t[j];
gor[j][sum] = true;
}
}
cin>>m2;
for(int i = ; i < m2; i++)
{
cin>>d;
gol[n][d] = true;
sum = d;
for(int j = n-; j >= ; j--)
{
sum += t[j+];
gol[j][sum] = true;
}
}
for(int i = ; i <= T; i++)
{
for(int j = ; j <= n; j++)
{
dp[i][j] = INF;
}
}
dp[][] = ;
for(int i = ; i <= T; i++)
{
for(int j = ; j <= n; j++)
{
if(i+ <= T) dp[i+][j] = min(dp[i][j]+,dp[i+][j]);
if(j+<=n&&gor[j][i]&&i+t[j+]<=T)dp[i+t[j+]][j+]=min(dp[i+t[j+]][j+],dp[i][j]);
if(j->=&&gol[j][i]&&i+t[j]<=T)dp[i+t[j]][j-]=min(dp[i+t[j]][j-],dp[i][j]);
}
}
printf("Case Number %d: ",++ca);
if(dp[T][n] >= INF) ans = -;
else ans = dp[T][n];
if(ans == -) printf("impossible\n");
else printf("%d\n",ans);
}
return ;
}

World Finals 2003 UVA - 1025 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. UVA 1025 -- A Spy in the Metro (DP)

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

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

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

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

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

  5. uva 1025 A Spy int the Metro

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

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

  7. UVa 1025 A Spy in the Metro

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 预处理出每个时间.每个车站是否有火车 为了方便判断是否可行,倒推处理 ...

  8. DAG的动态规划 (UVA 1025 A Spy in the Metro)

    第一遍,刘汝佳提示+题解:回头再看!!! POINT: dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间: 终点的状态很确定必然是的 dp[T][N] = 0 ---即在 ...

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

    题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...

随机推荐

  1. Web调用安卓,苹果手机摄像头,本地图片和文件

    由于要给一个客户做一个记账WAP,里面有调用手机拍照功能,这里记录一下,以供需要的朋友,下面是完整的一个HTML页面内容,放在服务器上然后浏览就可以了,只支持Chrome和Safari核的浏览器,我测 ...

  2. LYNC2013介绍和基础架构准备角色

    LYNC2013部署系列PART1:LYNC2013介绍和基础架构准备 前言:LYNC 2013发布已经很久了,本人一直在进行相关的学习和测试,在有限的资源条件下,把能够模拟出来的角色进行了安装部署, ...

  3. 已知整数m,n,p,q适合(m-p)|(mn+pq)证明:(m-p)|(mq+np)(整除理论1.1.5)

    已知整数m,n,p,q适合(m-p)|(mn+pq)证明:(m-p)|(mq+np) 证明: 令(mn+pq)—(mq+np) =mn-np+pq-mq =n(m-p)+q(p-m) =(n-q)(m ...

  4. SQL函数学习(一):substring()函数

    秒懂例子: substring('98765',-1,3) 结果:9 substring('98765',0,3) 结果:98 substring('98765',1,3) 结果:987 在操作sql ...

  5. mysql建表设置两个默认CURRENT_TIMESTAMP的技巧

    转载:http://blog.163.com/user_zhaopeng/blog/static/166022708201252323942430/   业务场景: 例如用户表,我们需要建一个字段是创 ...

  6. MyBatis 批量修改记录

    <insert id="update" parameterType="java.util.List"> UPDATE setting SET con ...

  7. (1)Two Sum--求数组中相加为指定值的两个数

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  8. 五、oracle 表的管理

    一.表名和列名的命名规则1).必须以字母开头2).长度不能超过30个字符3).不能使用oracle的保留字4).只能使用如下字符 a-z,a-z,0-9,$,#等 二.数据类型1).字符类char 长 ...

  9. SpringMvc处理post请求乱码的filter

    <filter>         <filter-name>encodingFilter</filter-name>         <filter-clas ...

  10. Eclipse/MyEclipse中常用快捷键总结

    1.格式化代码: Ctrl+Shift+F; 2.自动生成get/set方法:Shifi+Alt+S+R(按下空格是选中). 3.自动生成toString方法:Shifi+Alt+S+S(按下空格是选 ...