input:
1
3 8 17 20
0 10 8
0 10 13
4 14 3
output:
23
分析:
dp。以从左下方上来为例,dp[i][0]=第i块阶梯从离i最近且合法的左下方阶梯过来的最短时间
dp[i][0]=block[i].h-block[k].h+min(dp[k][0]+block[i].l-block[k].l,dp[k][1]+block[k].r-block[i].l)
右下方同理。
code:
#define frp
//#include<bits/stdc++.h>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const int maxn = 1000;
const int MAXN = 20050;
struct node {
int l, r, h;
node(int x1 = 0, int x2 = 0, int height = 0) : l(x1), r(x2), h(height) {}
bool operator<(const node &a) const {
return h < a.h;
}
} block[MAXN];
int n, x, y, MAXH;
int dp[MAXN][2];
bool inside(int x, int l, int r) {
return x >= l && x <= r;
}
void turnLeftOrRight(int i, int j) {
int k = i - 1;
while (k > 0 && block[i].h - block[k].h <= MAXH) {
if (j == 0 &&inside(block[i].l,block[k].l,block[k].r)) {
dp[i][0] = block[i].h - block[k].h +
min(dp[k][0] + block[i].l - block[k].l, dp[k][1] + block[k].r - block[i].l);
return;
} else if (j == 1 &&inside(block[i].r,block[k].l,block[k].r)) {
dp[i][1] = block[i].h - block[k].h +
min(dp[k][0] + block[i].r - block[k].l, dp[k][1] + block[k].r - block[i].r);
return;
}
k--;
}
if (block[i].h - block[k].h <= MAXH) {
dp[i][j] = block[i].h;
} else {
dp[i][j] = inf;
}
}
void solve() {
int t;
cin >> t;
while (t--) {
cin >> n >> x >> y >> MAXH;
block[0] = node(x, x, y);
for (int i = 1; i < n + 1; i++) {
int l, r, h;
cin >> l >> r >> h;
block[i] = node(l, r, h);
}
block[n + 1] = node(-inf, inf, 0);
sort(block, block + n + 2);
for (int i = 1; i <= n + 1; i++) {
turnLeftOrRight(i, 0);
turnLeftOrRight(i, 1);
}
cout << min(dp[n + 1][0], dp[n + 1][1]) << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef frp
freopen("D:\\coding\\c_coding\\in.txt", "r", stdin);
// freopen("D:\\coding\\c_coding\\out.txt", "w", stdout);
#endif
solve();
return 0;
}