dp[i][j], j = 0, 1 (dp[i][0]表示以i号平台左边为起点到地面的最短时间,dp[i][1]]表示以i号平台右边为起点到地面的最短时间)
状态转移方程:
dp[i][0] = H[i] - H[m] + Min (dp[m][0] + X1[i] - X1[m], dp[m][1] + X2[m] - X1[i]); m为i左边下面的平台的编号
dp[i][1] = H[i] - H[m] + Min (dp[m][0] + X2[i] - X1[m], dp[m][1] + X2[m] - X2[i]); m为i右边下面的平台的编号
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
//#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int M = 1050;
struct node
{
int x1, x2;
int high;
bool operator < (const node &b)const
{
return high < b.high;
}
}a[M];
int dp[M][2];
int N, X, Y, MAX;
void left(int i)
{
int k = i - 1;
while (k>0 && a[i].high - a[k].high <= MAX)
{
if (a[i].x1 >= a[k].x1 && a[i].x1 <= a[k].x2)
{
dp[i][0] = a[i].high - a[k].high + min(a[i].x1 - a[k].x1 + dp[k][0], a[k].x2 - a[i].x1 + dp[k][1]);
return;
}
else
{
k--;
}
}
if (a[i].high - a[k].high > MAX)
{
dp[i][0] = INF;
}
else
{
dp[i][0] = a[i].high;
}
}
void right(int i)
{
int k = i - 1;
while (k > 0 && a[i].high - a[k].high <= MAX)
{
if (a[i].x2 >= a[k].x1 && a[i].x2 <= a[k].x2)
{
dp[i][1] = a[i].high - a[k].high + min(a[i].x2 - a[k].x1 + dp[k][0], a[k].x2 - a[i].x2 + dp[k][1]);
return;
}
else
{
k--;
}
}
if (a[i].high - a[k].high > MAX)
{
dp[i][1] = INF;
}
else
{
dp[i][1] = a[i].high;
}
}
int gao()
{
for (int i = 1; i <= N + 1; ++i)
{
left(i);
right(i);
}
return min(dp[N + 1][0], dp[N + 1][1]);
}
int main()
{
int t;
cin >> t;
while (t--)
{
//memset(dp, 0, sizeof(dp));
cin >> N >> X >> Y >> MAX;
for (int i = 1; i <= N; ++i)
{
scanf("%d%d%d", &a[i].x1, &a[i].x2, &a[i].high);
}
a[0].x1 = -20000;
a[0].x2 = 20000;
a[0].high = 0;
a[N + 1].x1 = X;
a[N + 1].x2 = X;
a[N + 1].high = Y;
sort(a, a + N + 2);
//cout << "debug" << endl;
cout << gao() << endl;
}
return 0;
}