Help Jimmy[dp]

在这里插入图片描述

#include <iostream>
#include <string>
#include <math.h>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
const int N = 1010;
const int inf = 0x3f;
struct time
{
	int x1, x2, h;
}a[N];
int dp[N][2];

bool cmp(time a, time b)
{
	return a.h < b.h;
}
int main()
{
	int t, x, y, i, n, max_h;
	cin >> t;
	while (t--)
	{
		cin >> n >> x >> y >> max_h;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i].x1 >> a[i].x2 >> a[i].h;
		}
		sort(a + 1, a + n + 1,cmp);
		a[n + 1].x1 = a[n + 1].x2 = x;
		a[n + 1].h = y;
		a[0].x1 = -inf, a[0].x2 = inf, a[0].h = 0;//将出发点和地面当做一个平台
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n+1; i++)
		{
			int lflag = 0, rflag = 0;
			for (int j = i - 1; j >= 0 && a[i].h - a[j].h <= max_h; j--)
			{
				if (a[j].x1 <= a[i].x1 && a[j].x2 >= a[i].x1)
				{
					lflag = 1;
					if (j == 0)
						dp[i][0] = a[i].h;
					else
						dp[i][0] = a[i].h - a[j].h + min(dp[j][0] + a[i].x1 - a[j].x1, dp[j][1] + a[j].x2 - a[i].x1);
					break;
				}
			}
			if (lflag == 0)
				dp[i][0] = inf;
			for (int j = i - 1; j >= 0 && a[i].h - a[j].h <= max_h; j--)
			{
				if (a[j].x2 >= a[i].x2 && a[j].x1 <= a[i].x2)
				{
					rflag = 1;
					if (j == 0)
						dp[i][1] = a[i].h;
					else
						dp[i][1] = a[i].h - a[j].h + min(dp[j][1] + a[j].x2 - a[i].x2, dp[j][0] + a[i].x2 - a[j].x1);
					break;
				}
			}
			if (rflag == 0)
				dp[i][1] = inf;
		}
		cout << min(dp[n + 1][0], dp[n + 1][1]) << endl;
	}
}
posted @ 2020-08-25 18:09  _Hsiung  阅读(45)  评论(0编辑  收藏  举报