题目链接: https://vjudge.net/problem/UVA-1025

题解:

详情请看紫书P267。 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写。

递推:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 200+10; int n, T, M1, M2;
int t[maxn], has_train[maxn][maxn][2], dp[maxn][maxn];
int kase = 0; void init()
{
ms(t,0);
ms(has_train,0);
ms(dp,0); scanf("%d",&T);
for(int i = 1; i<n; i++)
scanf("%d",&t[i]); int M1, x;
scanf("%d",&M1);
for(int i = 1; i<=M1; i++)
{
scanf("%d",&x);
for(int j = 1; j<=n && x<=T; j++)
{
has_train[x][j][0] = 1;
x += t[j];
}
} int M2;
scanf("%d",&M2);
for(int i = 1; i<=M2; i++)
{
scanf("%d",&x);
for(int j = n; j>=1 && x<=T; j--)
{
has_train[x][j][1] = 1;
x += t[j-1];
}
}
} void solve()
{
for(int i = 1; i<=n-1; i++)
dp[i][j] = INF; dp[T][n] = 0;
for(int i = T-1; i>=0; i--)
for(int j = n; j>=1; j--)
{
dp[i][j] = dp[i+1][j] + 1; if(j<n && has_train[i][j][0] && i+t[j]<=T)
dp[i][j] = min(dp[i][j], dp[i+t[j]][j+1]); if(j>1 && has_train[i][j][1] && i+t[j-1]<=T)
dp[i][j] = min(dp[i][j], dp[i+t[j-1]][j-1]);
} printf("Case Number %d: ",++kase);
if(dp[0][1]<INF)
printf("%d\n",dp[0][1]);
else
puts("impossible");
} int main()
{
while(scanf("%d",&n) && n)
{
init();
solve();
}
return 0;
}

记忆化搜索:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 200+10; int n, T, M1, M2;
int t[maxn], has_train[maxn][maxn][2], dp[maxn][maxn];
int kase = 0; void init()
{
ms(t,0);
ms(has_train,0);
ms(dp,-1); scanf("%d",&T);
for(int i = 1; i<n; i++)
scanf("%d",&t[i]); int M1, x;
scanf("%d",&M1);
for(int i = 1; i<=M1; i++)
{
scanf("%d",&x);
for(int j = 1; j<=n && x<=T; j++)
{
has_train[x][j][0] = 1;
x += t[j];
}
} int M2;
scanf("%d",&M2);
for(int i = 1; i<=M2; i++)
{
scanf("%d",&x);
for(int j = n; j>=1 && x<=T; j--)
{
has_train[x][j][1] = 1;
x += t[j-1];
}
} for(int i = 1; i<n; i++)
dp[T][i] = INF;
dp[T][n] = 0;
} int dfs(int i, int j)
{
if(dp[i][j]!=-1) return dp[i][j]; dp[i][j] = dfs(i+1, j) + 1; if(j<n && has_train[i][j][0] && i+t[j]<=T)
dp[i][j] = min( dp[i][j], dfs( i+t[j], j+1 ) ); if(j>1 && has_train[i][j][1] && i+t[j-1]<=T)
dp[i][j] = min( dp[i][j], dfs( i+t[j-1], j-1 ) ); return dp[i][j];
} int main()
{
while(scanf("%d",&n) && n)
{
init();
dfs(0,1); printf("Case Number %d: ",++kase);
if(dp[0][1]<INF)
printf("%d\n",dp[0][1]);
else
puts("impossible");
}
return 0;
}

UVA1025 A Spy in the Metro —— DP的更多相关文章

  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. 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍

    参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...

  4. Uva1025 A Spy in the Metro

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...

  5. 题解:UVa1025 A Spy in the Metro

    原题链接 pdf 题目大意 给出一张无向图图,求该图的最小瓶颈生成树. 无向图的瓶颈生成树:无向图\(G\)的一颗瓶颈生成树是这样的一颗生成树:它最大的边权值在\(G\)的所有生成树中是最小的.瓶颈生 ...

  6. 【Uva1025 A Spy in the Metro】动态规划

    题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...

  7. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  8. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

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

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

随机推荐

  1. Software Engineering | Strategy pattern

    聚合关系.

  2. Nginx三种模式的虚拟主机(附Apache基于域名的虚拟主机)

    1.安装nginx # pcre中文"perl兼容正则表达式",安装pcre库是为了让nginx支持具备URL重写功能 # 的Rewrite模块,rewrite可以实现动态页面转成 ...

  3. Redis集群设计原理

    ---恢复内容开始--- Redis集群设计包括2部分:哈希Slot和节点主从,本篇博文通过3张图来搞明白Redis的集群设计. 节点主从: 主从设计不算什么新鲜玩意,在数据库中我们也经常用主从来做读 ...

  4. BZOJ 3065 带插入区间第K小值

    题目描述 Description 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间k小值. ...

  5. 【linux】CentOS编译程序报错 修复 ./Modules/_ssl.c:64:25: 致命错误:openssl/rsa.h:没有那个文件或目录

    如果你在编译时遇到这个错误,这可能是下面的原因:你尝试编译的程序使用OpenSSL,但是需要和OpenSSL链接的文件(库和头文件)在你Linux平台上缺少. 所以在CentOS下, 退到根路径,[需 ...

  6. const mutable

    在C++中,由const修饰的成员函数的函数体内部,是不能够对成员变量进行修改的.这个特性被用来保证某些成员函数在实现过程中,避免由于程序员大意而对数据进行了错误的修改:同时也说明此成员函数是非修改性 ...

  7. NN优化方法对照:梯度下降、随机梯度下降和批量梯度下降

    1.前言 这几种方法呢都是在求最优解中常常出现的方法,主要是应用迭代的思想来逼近.在梯度下降算法中.都是环绕下面这个式子展开: 当中在上面的式子中hθ(x)代表.输入为x的时候的其当时θ參数下的输出值 ...

  8. S3C2440 IIS操作 uda134x录放音

    IIS(Inter-IC Sound)由飞利浦公司开发.是一种经常使用的音频设备接口,主要用于CD.MD.MP3等设备. s3c2440一共同拥有5个引脚用于IIS:IISDO.IISDI.IISSC ...

  9. xcode 5.0 以上去掉icon高亮方法&amp;iOS5白图标问题

    之前的建议方法是把在xxx.info.plist文件里把 icon already includes gloss and bevel effects 设置YES 在Xcode5下,重复实现不成功,今天 ...

  10. dubbo学习之Hello world

    现在企业中使用dubbo的越来越多,今天就简单的学习一下dubbo,写了一个hello world,教程仅供入门,如要深入学习请上官网 服务提供方: 首先将提供方和消费方都引入jar包,如果使用的是m ...