传送门

参考资料:

  [1]:算法竞赛入门经典:第九章 DAG上的动态规划

题意:

  Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过;

  其中 M1 辆列车从 1 号站台驶向 n 号站台,M2 辆列车从 n 号站台驶向 1 号地铁;

  (单程线,M1 辆列车到达 n 号站台后不会回返,同理 M2)

  特工 Maria 要在 T 时刻到达 n 号站台与特务会面,但为了保证安全,在会面前尽量呆在行进的列车中;

  现给出你这 M1+M2 辆列车的发车时刻;

  问如何换乘列车使得特工 Maria 能在 T 时刻前到达 n 号站台,并且在换乘期间在站台的停留时间最短;

  如果可以在规定时间到达 n 站台,输出在站台停留的最短时间,反之,输出 "impossible";

题解:

  看完书上的解释后,感觉,不像是DAG上的动态规划,倒有点像背包的味道;

 int n,t;
int m1,m2;
int f[maxn];///前m1辆列车的发车时刻
int e[maxn];///后m2辆列车的发车时刻
int c[maxn];///c[i]:车站i到车站i+1的时间花费
/**
(i,j):i时刻在车站j
dp[i][j]:从(i,j)->(t,n)所需等待的最少时间
*/
int dp[maxn][];
/**
hasTrain[i][j][0]=true:i时刻在车站j有到j+1的火车
hasTrain[i][j][1]=true:i时刻在车站j有到j-1的火车
*/
bool hasTrain[maxn][][];

  最关键的便是dp[ i ][ j ]的定义;

  之所以定义成二维的,是因为决策受当前时间和所处车站的影响,有两个影响因素;

  定义好后,便是找状态转移方程了;

  首先预处理出 hasTrain 数组:

 void Init()///预处理hasTrain
{
mem(hasTrain,false);
for(int i=;i <= m1;++i)
{
int cnt=f[i];
hasTrain[cnt][][]=true;
for(int j=;j <= n;++j)
{
cnt += c[j-];
hasTrain[cnt][j][]=true;
}
}
for(int i=;i <= m2;++i)
{
int cnt=e[i];
hasTrain[cnt][n][]=true;
for(int j=n-;j >= ;--j)
{
cnt += c[j];
hasTrain[cnt][j][]=true;
}
}
}

预处理hasTrain[]

  令dp[t][n]=0,dp[t][1,2,...,n-1]=INF;

  按照时间逆序遍历,对于状态 dp[ i ][ j ]:

  ①等一分钟,下一分钟从车站 j 出发到达(t , n);

  ②搭乘往右开的列车;

  ③搭乘往左开的列车;

  

AC代码:

 #include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=+; int n,t;
int m1,m2;
int f[maxn];///前m1辆列车的发车时刻
int e[maxn];///后m2辆列车的发车时刻
int c[maxn];///c[i]:车站i到车站i+1的时间花费
/**
(i,j):i时刻在车站j
dp[i][j]:从(i,j)->(t,n)所需等待的最少时间
*/
int dp[maxn][];
/**
hasTrain[i][j][0]=true:i时刻在车站j有到j+1的火车
hasTrain[i][j][1]=true:i时刻在车站j有到j-1的火车
*/
bool hasTrain[maxn][][]; void Init()///预处理hasTrain
{
mem(hasTrain,false);
for(int i=;i <= m1;++i)
{
int cnt=f[i];
hasTrain[cnt][][]=true;
for(int j=;j <= n;++j)
{
cnt += c[j-];
hasTrain[cnt][j][]=true;
}
}
for(int i=;i <= m2;++i)
{
int cnt=e[i];
hasTrain[cnt][n][]=true;
for(int j=n-;j >= ;--j)
{
cnt += c[j];
hasTrain[cnt][j][]=true;
}
}
}
void Solve()
{
Init();
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 && hasTrain[i][j][] && i+c[j] <= t)
dp[i][j]=min(dp[i][j],dp[i+c[j]][j+]);
if(j > && hasTrain[i][j][] && i+c[j-] <= t)
dp[i][j]=min(dp[i][j],dp[i+c[j-]][j-]);
}
}
if(dp[][] >= INF)
puts("impossible");
else
printf("%d\n",dp[][]);
}
int main()
{
int kase=;
while(~scanf("%d",&n) && n)
{
scanf("%d",&t);
for(int i=;i < n;++i)
scanf("%d",c+i);
scanf("%d",&m1);
for(int i=;i <= m1;++i)
scanf("%d",f+i);
scanf("%d",&m2);
for(int i=;i <= m2;++i)
scanf("%d",e+i); printf("Case Number %d: ",++kase);
Solve();
}
return ;
}

UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)的更多相关文章

  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 437 The Tower of Babylon(DAG上的动态规划)

    题目大意是根据所给的有无限多个的n种立方体,求其所堆砌成的塔最大高度. 方法1,建图求解,可以把问题转化成求DAG上的最长路问题 #include <cstdio> #include &l ...

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

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

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

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

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

  8. uva 1025 A Spy int the Metro

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

  9. UVa 1025 A Spy in the Metro

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

随机推荐

  1. Liferay 7 OSGi第三方jar包依赖问题

    发现遇到这个问题的人挺多的,现在跟新一下好好写. 在开发Liferay的时候,我们常常会遇到Unresolved requirement: Import-Package: {package name} ...

  2. 【软件安装】python安装numpy、scipy

    python2.7开发环境,若为python3.4的环境则下载对应的软件 系统为64为windows环境,显然不同于32的环境,更繁琐,所谓的网友教程也不尽人意. 安装numpy 下载地址:http: ...

  3. org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.trs.om.bean.User.retryCount

    六月 29, 2019 5:42:45 下午 org.apache.catalina.core.AprLifecycleListener init信息: The APR based Apache To ...

  4. python字典排序取最值总结

    dic = {"abc":18,"adc":19,"abe":20} # 默认对键排序,从小到大,返回排序后键组成的列表 zidian = ...

  5. poj3261 后缀数组求重复k次可重叠的子串的最长长度

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13669   Accepted: 6041 Ca ...

  6. hdu2041 dp

    #include<stdio.h> int main() { int i,t,n; ]; dp[]=; dp[]=; dp[]=; ;i<=;i++) dp[i]=dp[i-]+dp ...

  7. Python与Java异常类层级区别

  8. [转]The Curse of Dimensionality(维数灾难)

    原文章地址:维度灾难 - 柳枫的文章 - 知乎 https://zhuanlan.zhihu.com/p/27488363 对于大多数数据,在一维空间或者说是低维空间都是很难完全分割的,但是在高纬空间 ...

  9. Inventor安装失败怎样卸载重新安装Inventor,解决Inventor安装失败的方法总结

    技术帖:Inventor没有按照正确方式卸载,导致Inventor安装失败.楼主也查过网上关于如何解决Inventor安装失败的一些文章,是说删除几个Inventor文件和Inventor软件注册表就 ...

  10. 巧用 PHP 数组函数

    0x00 前言 PHP 的数组是一种很强大的数据类型,与此同时 PHP 内置了一系列与数组相关的函数可以很轻易的实现日常开发的功能.但是我发现好像很多小伙伴都忽略了内置函数的作用(比如我自己就编写过一 ...