传送门

参考资料:

  [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. input的表单验证(不断更新中~~)

    1 手机号验证 <input type="tel" id="phone" name="phone" placeholder=" ...

  2. Java中的四种引用(强引用、软引用、弱引用、虚引用)

    以下内容摘自<深入理解Java虚拟机 JVM高级特性与最佳实践>第2版,强烈推荐没有看过的同学阅读,读完的感觉就是"原来学的都是些什么瘠薄东西(╯‵□′)╯︵┴─┴" ...

  3. python 缺失值处理

  4. pl/sql基础知识—定义并使用变量

    n  介绍 在编写pl/sql程序是,可以定义变量和常量:在pl/sql程序中包括有: ①标量类型(scalar) ②复合类型(composite) ③参照类型(reference) ④lob(lar ...

  5. python ASCII编码集

  6. Java练习 SDUT-4303_简单的复数运算(类和对象)

    简单的复数运算(类和对象) Time Limit: 2000 ms Memory Limit: 65536 KiB Problem Description 设计一个类Complex,用于封装对复数的下 ...

  7. HZOJ 简单的期望

    性质:一个数分解质因数后2的次数=二进制下末尾连续0的个数. 乘2比较好考虑,比较恶心的是+1.一个$k*2^0$的数+1后可能会出现很多情况.但是k这个数表示不出来. 但是加的操作最多有200次,也 ...

  8. 2018-12-25-C#-使用转换语义版本号

    title author date CreateTime categories C# 使用转换语义版本号 lindexi 2018-12-25 09:25:41 +0800 2018-06-29 12 ...

  9. 爬虫:Selenium + PhantomJS

    更:Selenium特征过多(language/UserAgent/navigator/en-US/plugins),以Selenium打开的浏览器处于自测模式,很容易被检测出来,解决方法可选: 用m ...

  10. 1x1卷积

    你可能会想为什么有人会用1x1卷积,因为它关注的不是一块像素,而是一个像素,图1 图1 我们看看传统的卷积,它基本上是运行在一个小块图像上的小分类器,但仅仅是个线性分类器.图2 图2 如果你在中间加一 ...