UVA1025 A Spy in the Metro —— DP
题目链接: 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的更多相关文章
- 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 ...
- UVA 1025 -- A Spy in the Metro (DP)
UVA 1025 -- A Spy in the Metro 题意: 一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...
- Uva1025 A Spy in the Metro
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...
- 题解:UVa1025 A Spy in the Metro
原题链接 pdf 题目大意 给出一张无向图图,求该图的最小瓶颈生成树. 无向图的瓶颈生成树:无向图\(G\)的一颗瓶颈生成树是这样的一颗生成树:它最大的边权值在\(G\)的所有生成树中是最小的.瓶颈生 ...
- 【Uva1025 A Spy in the Metro】动态规划
题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...
- 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)
洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...
- UVA1025-A Spy in the Metro(动态规划)
Problem UVA1025-A Spy in the Metro Accept: 713 Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...
- uva 1025 A Spy in the Metro 解题报告
A Spy in the Metro Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug Secr ...
随机推荐
- 洛谷——P1144 最短路计数
P1144 最短路计数 题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M,为图的顶 ...
- win7快捷键和ubuntu快捷键
http://www.cnblogs.com/xfiver/archive/2010/12/08/1899905.html http://www.pc841.com/article/20121203- ...
- Codeforces Gym 100431D Bubble Sort 水题乱搞
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- [JSOI 2015] 子集选取
4475: [Jsoi2015]子集选取 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 363 Solved: 255[Submit][Status] ...
- 安装Vmware增强工具
主机: Win7 虚拟机: VMware8.0+Debian6 目标: 离线安装软件包和VMware Tools 在虚拟机上安装完debian6后 1.在vmware的菜单中选择Vm->inst ...
- 一个jdbc connection连接对应一个事务
Spring保证在methodB方法中所有的调用都获得到一个相同的连接.在调用methodB时,没有一个存在的事务,所以获得一个新的连接,开启了一个新的事务. Spring保证在methodB方法中所 ...
- sql 表的部分字段查找 的结果集
传统sql从多个对象中获得的list<Object> ,可以这样处理(利用Map) List list = query.getList(sql); //封装成BB类型 List< ...
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)
非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...
- navicat for mysql 快捷键(原创)
navicat for mysql 快捷键(原创) 在谷歌,百度上基本搜索不出来这方面的内容,我总结了一下,方便新手,节省一些探索的时间. 1.ctrl+q 打开查询窗口2.ctr ...
- hdu 1081 & poj 1050 To The Max(最大和的子矩阵)
转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...