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. myeclipse启动tomcat报错cannot find a free socket for debugger

    解决办法,命令行输入netsh winsock reset,winsock是Windows网络编程接口,winsock工作在应用层,它提供与底层传输协议无关的高层数据传输编程接口 netsh wins ...

  2. EDIUS设置3D转场的教程

    当你们学习完了EDIUS 2D转场自定义后,一定也想了解3D是如何自定义的吧!其实用法都差不多,下面就和小编我一起来学习下EDIUS 3D转场自定义设置吧! 1.首先我们还是做好自定义转场的准备工作. ...

  3. js获取当前时间&js 页面时钟

    js获取当前时间 //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = ...

  4. C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)

    1.  主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...

  5. Android 另类方法监听软键盘的弹出收起事件

    http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...

  6. php 二维数组(没啥技术含量)

    <?php $cars = array( array('benchi',20,18), array('baoma',30,21), array('aodi',23,9) ); echo $car ...

  7. SSH登陆 Write failed: Broken pipe解决办法

    新装的一台linux 6.4主机在所有参数调优以后,运行起来要跑的程序后.再通过su - www时,提示如下: su: cannot set user id: Resource temporarily ...

  8. Python基础篇【第2篇】: Python文件操作

    Python文件操作 在Python中一个文件,就是一个操作对象,通过不同属性即可对文件进行各种操作.Python中提供了许多的内置函数和方法能够对文件进行基本操作. Python对文件的操作概括来说 ...

  9. 解决:jquery ajax非首次请求Server端获取cookie值中文乱码问题

    HttpCookie cookie = new HttpCookie("RealName", HttpUtility.UrlEncode("你想要设置的值")) ...

  10. 创建线程方式-GCD

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...