题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2155
思路:大神地址:http://www.aiuxian.com/article/p-1671720.html
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const double inf = 2100000;
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
double dp[1010][2];
struct node
{
double left;
double right;
double height;
} a[1010];
bool cmp(node x,node y)
{
if(x.height != y.height)
return x.height > y.height;
else
return x.left < y.left;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
double x,y,max1,m;
scanf("%d%lf%lf%lf%lf",&n,&x,&y,&max1,&m);
a[0].left = a[0].right = x;
a[0].height = y;
for(int i=1; i<=n; i++)
scanf("%lf%lf%lf",&a[i].left,&a[i].right,&a[i].height);
sort(a+1,a+n+1,cmp);
a[n+1].left = 0;
a[n+1].right = 1000;
a[n+1].height = 0;
for(int i=1; i<=n+1; i++)
dp[i][0] = dp[i][1] = inf;
dp[0][0] = 0;
dp[0][1] = 0;
for(int i=1; i<=n+1; i++)
{
for(int j=0; j<i; j++)
{
if(a[i].height - a[j].height > max1) break;
if(a[j].left >= a[i].left && a[j].left <= a[i].right)
{
if(i == n+1)
{
dp[i][0] = min(dp[i][0],dp[j][0] + a[j].height);
//printf("%lf\n",dp[i][0]);
dp[i][1] = min(dp[i][1],dp[j][1] + a[j].height);
//printf("%lf\n",dp[i][1]);
}
else
{
dp[i][0] = min(dp[i][0],dp[j][0] + a[j].left - a[i].left + a[j].height - a[i].height);
dp[i][1] = min(dp[i][1],dp[j][0] + a[i].right - a[j].left + a[j].height - a[i].height);
}
}
}
for(int j=0; j<i; j++)
{
if(a[i].height - a[j].height > max1) break;
if(a[i].right >= a[j].left && a[i].right <= a[j].right)
{
if(i == n+1)
{
dp[i][0] = min(dp[i][0],dp[j][0] + a[j].height);
//printf("%lf\n",dp[i][0]);
dp[i][1] = min(dp[i][1],dp[j][1] + a[j].height);
//printf("%lf\n",dp[i][1]);
}
else
{
dp[i][0] = min(dp[i][0],dp[j][1] + a[j].right - a[i].left + a[j].height - a[i].height);
dp[i][1] = min(dp[i][1],dp[j][1] + a[i].right - a[j].right + a[j].height - a[i].height);
}
}
}
//printf("%d %lf %lf\n",i,dp[i][0],dp[i][1]);
}
if(dp[n+1][0] <= m || dp[n+1][1] <= m)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}