POJ1661(DP)
来源:互联网 发布:听音识曲软件 编辑:程序博客网 时间:2024/06/07 07:14
题目链接:http://poj.org/problem?id=1661
把起点当做从X到X高度为Y的平台,地面当做从-20000到20000高度为0的平台,所有平台按照高度从大到小排序。用f[i][0]表示从第 i 个平台的左端到达地面所需要的最短时间,f[i][1]表示从第 i 个平台的右端到达地面的时间,那么f[i][0] = min(f[j][0]+a[i].x1-a[j].x1,f[j][1]+a[j].x2-a[i].x1)+a[i].h-a[j].h; f[i][1] = min(f[j][0]+a[i].x2-a[j].x1,f[j][1]+a[j].x2-a[i].x2)+a[i].h-a[j].h;如过超过最大高度的话就设为极大值。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = (1<<29)-1;struct node{ int x1, x2; int h;};node a[1010];int f[1010][2];int N, X, Y, MAX;int cmp(const node& a, const node& b){ return a.h > b.h;}int main(){ int t; scanf("%d",&t); while (t > 0) { t--; memset(f,0,sizeof(f)); scanf("%d%d%d%d",&N,&X,&Y,&MAX); for (int i=1; i<=N; i++) { scanf("%d%d%d",&a[i].x1,&a[i].x2,&a[i].h); } a[0].x1 = X; a[0].x2 = X; a[0].h = Y; a[N+1].x1 = -20000; a[N+1].x2 = 20000; a[N+1].h = 0; sort(a,a+N+1,cmp); //不讲道理,把排序放在地面平台之前排序就错了,地面平台肯定是最后一个呀 for (int i=N; i>=0; i--) { int j = i + 1; while (j < N+1 && !(a[j].x1 <= a[i].x1 && a[j].x2 >= a[i].x1)) j++; if (a[i].h-a[j].h <= MAX) { if (j < N+1) f[i][0] = min(f[j][0]+a[i].x1-a[j].x1,f[j][1]+a[j].x2-a[i].x1)+a[i].h-a[j].h; else f[i][0] = a[i].h; } else f[i][0] = 2000000; j = i + 1; while (j < N+1 && !(a[j].x1 <= a[i].x2 && a[j].x2 >= a[i].x2)) j++; if (a[i].h-a[j].h <= MAX) { if (j < N+1) f[i][1] = min(f[j][0]+a[i].x2-a[j].x1,f[j][1]+a[j].x2-a[i].x2)+a[i].h-a[j].h; else f[i][1] = a[i].h; } else f[i][1] = 2000000; } printf("%d\n",f[0][0]); } return 0;}
0 0
- POJ1661(DP)
- poj1661 Help Jimmy(DP)
- [DP] POJ1661
- POJ1661 Help Jimmy DP (待解决)
- poj1661 经典dp
- dp 略难 poj1661
- poj1661【DP,左右两端dp】
- poj1661 Help Jimmy-dp动态规划 (M)
- poj1661
- poj1661
- POJ1661
- poj1661 Help Jimmy-dp动态规划
- POJ1661——Help Jimmy(关于动态规划)
- POJ1661——Help Jimmy(动态规划)
- POJ1661 神坑卧槽!!
- poj1661 记忆化搜索
- poj1661 Word Puzzles
- poj1661 Help Jimmy
- HMM预测算法——Viterbi算法
- 字符串切分,String和StringUtils的区别
- java容器 -- list的总结
- js this的用法
- PowerShell-Suite
- POJ1661(DP)
- fopen、fseek、fread、fclose
- js onclick
- 框架源码 — 可能会有趣一点地简析学习 Retrofit
- Android外部存储
- apt-get 三个update相关的命令
- 能够排序的Map实现类TreeMap类
- C# List<T>用法
- 开机设置U盘启动