题目链接: 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. Network | DHCP

    动态主机设置协议(Dynamic Host Configuration Protocol, DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途: 给内部网络或网络服务供应商自动分配I ...

  2. HttpClient的Post请求数据

    最近在项目中需要添加Post请求数据,以前的Get请求是使用JDK自带的URLConnection.在项目组人员的推荐下,开始使用HttpClient. HttpClient简介: HttpClien ...

  3. 简约至上.md

    中秋花了一天多时间阅读了简约至上这本书,书中内容不多,主要是向我们传达了产品设计的4个要素,给了产品经理设计产品时的一些要义指导; 一产品定位 在进行产品设计之前,首页需要对这款产品的商业定位需要有个 ...

  4. WCF中常用的binding方式 z

    WCF中常用的binding方式: BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务.用于兼容旧的Web ASMX 服务. WSHttpBinding: 比 Bas ...

  5. TCP通过滑动窗口和拥塞窗口实现限流,能抵御ddos攻击吗

    tcp可以通过滑动窗口和拥塞算法实现流量控制,限制上行和下行的流量,但是却不能抵御ddos攻击. 限流只是限制访问流量的大小,是无法区分正常流量和异常攻击流量的. 限流可以控制本软件或者应用的流量大小 ...

  6. 如何在Linux中使用sFTP上传或下载文件与文件夹

    如何在Linux中使用sFTP上传或下载文件与文件夹 sFTP(安全文件传输程序)是一种安全的交互式文件传输程序,其工作方式与 FTP(文件传输协议)类似. 然而,sFTP 比 FTP 更安全;它通过 ...

  7. bootstrap3分页

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  8. vue class绑定 组件

    当在一个自定义组件上使用 class 属性时,这些类将被添加到该组件的根元素上面.这个元素上已经存在的类不会被覆盖. 例如,如果你声明了这个组件: Vue.component('my-componen ...

  9. 无法安装 golang.org/x/tools/的库

    安装godep 官方的安装文档是使用go get github.com/tools/godep,很可惜,因为“网络”问题会报一个找不到golang.org/x/tools/go/vcs的错误. 而ht ...

  10. innerHTML outerHTML innerText

      迁移时间--2017年10月31日14:52:59 Author:Marydon UpdateTime--2017年1月15日20:33:03innerHTML,outerHTML与innerTe ...