| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 8955 | Accepted: 2871 |
Description

Input
Output
Sample Input
1 3 8 17 20 0 10 8 0 10 13 4 14 3
Sample Output
23
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
int n, x, y, MAX, t, i, j, ok;
struct node
{
int l, r, h;
}p[1010];
bool cmp(node a, node b)//低到高排序
{
return a.h <= b.h;
}
int dp[1010][2];
int main()
{
cin >> t;
while (t--)
{
cin >> n >> x >> y >> MAX;
for (i = 0; i < n; i++)
cin >> p[i].l >> p[i].r >> p[i].h;
sort(p,p+n,cmp);
p[n].l = x; p[n].r = x; p[n].h = y;
dp[0][0] = dp[0][1] = p[0].h;
//从下向上DP,dp[i][0] 左侧最短时间 / dp[i][1] 右侧最短时间
for (i = 1; i <= n; i++)
{
dp[i][0] = dp[i][1] = 100000000;
ok = 0;
for (j = i-1; j >=0 ; j--)
if (p[i].l >= p[j].l && p[i].l <= p[j].r)//左
{
ok = 1; break;
}
if (ok)
{
if (p[i].h - p[j].h <= MAX)
dp[i][0] = min(dp[j][0] + p[i].l - p[j].l, dp[j][1] + p[j].r - p[i].l) + p[i].h - p[j].h;
}
else if (p[i].h <= MAX)
dp[i][0] = p[i].h;
ok = 0;
for (j = i-1; j >=0 ; j--)
if (p[i].r >= p[j].l && p[i].r <= p[j].r)//右
{
ok = 1;
break;
}
if (ok)
{
if (p[i].h - p[j].h <= MAX)
dp[i][1] = min(dp[j][0] + p[i].r - p[j].l, dp[j][1] + p[j].r - p[i].r) + p[i].h - p[j].h;
}
else if(p[i].h <= MAX)
dp[i][1] = p[i].h;
}
cout << dp[n][0] << endl;
}
return 0;
}
原文:http://blog.csdn.net/u014427196/article/details/44058587