http://poj.org/problem?id=1661

对板按高度排序后。

dp[i][0]表示现在站在第i块板上,向左跑了,的状态,记录下时间和其他信息。

O(n^2)LIS;

唯一的麻烦就是,如果由第i块板---->第j块板,除了高度差会摔死之后,还可能会中间隔着一些板,使得它是去不了第j块的

所以用个vis标记下,如果i--->j中,vis[i]已经是true,表示第i块板已经被其他板截住了。判断一下就好。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e3 + ;
struct node {
int x, y, h;
bool operator < (const struct node & rhs) const {
if (h != rhs.h) return h > rhs.h;
else if (x != rhs.x) return x < rhs.x;
else return y < rhs.y;
}
} a[maxn];
struct DP {
int x, h, tim;
int cat;
} dp[maxn][];
void work() {
// init();
int n, x, y, limit;
scanf("%d%d%d%d", &n, &x, &y, &limit);
for (int i = ; i <= n; ++i) {
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].h);
}
n++;
a[n].x = -inf;
a[n].y = inf;
a[n].h = ;
memset(dp, 0x3f, sizeof dp);
dp[][].x = dp[][].x = x;
dp[][].h = dp[][].h = y;
dp[][].tim = dp[][].tim = ;
dp[][].cat = dp[][].cat = ;
for (int i = ; i <= n; ++i) {
dp[i][].cat = dp[i][].cat = ;
}
// cout << dp[3][1].cat << en
sort(a + , a + + n);
int ans = inf;
for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) {
if (dp[j][].h - a[i].h > limit) continue;
if (!dp[j][].cat) {
bool flag = true;
if (i == n) {
int cost = dp[j][].h + dp[j][].tim;
ans = min(ans, cost);
flag = false;
}
if (flag) {
if (dp[j][].x >= a[i].x && dp[j][].x <= a[i].y) {
dp[j][].cat = true;
int cost = dp[j][].x - a[i].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].h = a[i].h;
dp[i][].x = a[i].x;
}
cost = a[i].y - dp[j][].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].x = a[i].y;
dp[i][].h = a[i].h;
}
}
}
}
if (!dp[j][].cat) {
if (i == n) {
int cost = dp[j][].tim + dp[j][].h;
ans = min(ans, cost);
continue;
}
if (dp[j][].x >= a[i].x && dp[j][].x <= a[i].y) {
dp[j][].cat = true;
int cost = dp[j][].x - a[i].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].x = a[i].x;
dp[i][].h = a[i].h;
}
cost = a[i].y - dp[j][].x + dp[j][].h - a[i].h + dp[j][].tim;
if (dp[i][].tim > cost) {
dp[i][].tim = cost;
dp[i][].h = a[i].h;
dp[i][].x = a[i].y;
}
}
}
}
}
// cout << dp[3][1].cat << endl;
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int g;
cin >> g;
while (g--) work();
return ;
}

POJ 1661 Help Jimmy LIS DP的更多相关文章

  1. POJ - 1661 - Help Jimmy - 简单dp

    http://poj.org/problem?id=1661 一般化处理,把一开始的落地和大地都视作平台,设计平台类的属性.dp的时候显然是从上往下dp的,而且要小心Jimmy不能够穿过平台,也就是从 ...

  2. POJ 1661 Help Jimmy【DP】

    基础DP,过程想明白了其实也不复杂,从上面的推下面的比倒着推要简单很多.调试了半个多小时..简单dp依然不能快速AC..SAD.. 题目链接: http://poj.org/problem?id=16 ...

  3. POJ 1661 Help Jimmy(DP/最短路)

    Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14980 Accepted: 4993 Descripti ...

  4. POJ 1661 Help Jimmy(C)动态规划

    没刷过 POJ,这题是论坛有人问的,我才看看. 我发现 POJ 注册很奇怪,账号总是登不上去,弄的我还注册两个.Emmm 首次体验很差,还好我不在 POJ 刷题. 题目链接:POJ 1661 Help ...

  5. POJ 1661 Help Jimmy(递推DP)

    思路: 1. 每个板子有左右两端, dp[i][0], dp[i][1] 分别记录左右端到地面的时间 2. 从下到上递推计算, 上一层的板子必然会落到下面的某一层板子上, 或者地面上 总结: 1. 计 ...

  6. POJ 1661 Help Jimmy(二维DP)

    题目链接:http://poj.org/problem?id=1661 题目大意: 如图包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的 ...

  7. POJ 1661 Help Jimmy(DP,注意边界)

    Help Jimmy Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9399   Accepted: 3025 Descri ...

  8. POJ 1661 Help Jimmy DP

    思路:Jimmy 跳到一块板上后,可以有两种选择,向左走或向右走.走到左端和走到右端所需的时间,容易算出. n如果我们能知道,以左端为起点到达地面的最短时间,和以右端为起点到达地面的最短时间,那么向左 ...

  9. POJ 1661 Help Jimmy -- 动态规划

    题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...

随机推荐

  1. 递归输出文件夹下的所有文件的名称(转自 MSDN)

    问题:如何输出给定文件夹目录下面的所有文件的名称? C#代码: using System; using System.IO; namespace MyTest { public class Progr ...

  2. 基于MVC4+EasyUI的Web开发框架形成之旅--MVC控制器的设计

    自从上篇<基于MVC4+EasyUI的Web开发框架形成之旅--总体介绍>总体性的概括,得到很多同行的关注和支持,不过上一篇主要是介绍一个总体的界面效果和思路,本系列的文章将逐步介绍其中的 ...

  3. VB6对象与地址相互转换

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (lpDest As ...

  4. c++ poco库https调用

    #include "Poco\File.h"#include "Poco\FileStream.h"#include "Poco\Process.h& ...

  5. UIScroView 3倍的contentSize,左右Scroll时,懒惰加载View

    UIScroView 3倍的contentSize,左右Scroll时,懒惰添加左右的View 用途:分段加载数据 定义枚举: typedefenum { ViewPositionLeft = , V ...

  6. winform基本控件----按钮

    这次来引用一个我们上课时候老师给的一个实验内容,来说一下winform程序设计中的按钮控件的使用.下面是我们老师给的实验内容. 实验目的: 掌握Winform的开发环境. 掌握窗体的创建和基本方法. ...

  7. OpenStack nova VM migration (live and cold) call flow

    OpenStack nova compute supports two flavors of Virtual Machine (VM) migration: Cold migration -- mig ...

  8. python学习-day14:集合,函数,格式化

    一.集合 定义:由不同元素组成的集合.集合是一组无序排列的可hash值, 可以作为字典的key.元素必须是不可变类型:只能存放数字,字符串,字典 特性:集合的目的是将不同的值放在一起,不同的集合之间可 ...

  9. nodejs初探(四)实现一个多人聊天室

    我们实现的思路是,当有一个人发送过来消息,我们就广播给其他客户端. var net = require('net'); var chatServer = net.createServer(), cli ...

  10. Android API Guides 学习笔记---Application Fundamentals(一)

    今天开始学习google官网上的API guides ,主要读了Application Fundamentals这一章节,此章节介绍了一个App的基本组成,共包括四大部分内容. 1.      App ...