poj 1661

定义好状态.

代码:

#include<iostream>
#include<fstream>

using namespace std;

struct e{
	int left,right;
};

e dp[1010];

struct a{
	int x,y,h;
};

a map[1010];

int cmp(const void *p,const void *q){
	a *s=(a*)p;
	a *t=(a*)q;
	return t->h-s->h;
}
int n,maxx;

int solve(int s,int f){

	int x,y,h,i,j,k;
	if(f)
		x=map[s].x;
	else
		x=map[s].y;
	for(i=s+1;i<=n;i++)
		if(x>=map[i].x&&x<=map[i].y)
		{
			j=i;
			if(map[s].h-map[i].h>maxx)
				return 30000;
			break;
		}
	if(i>n)
		if(map[s].h>maxx)
			return 30000;
		else
			return map[s].h;
	int left,right;
	left=map[s].h-map[i].h+x-map[i].x;
	right=map[s].h-map[i].h+map[i].y-x;
	if(!dp[i].left)
		dp[i].left=solve(i,true);
	if(!dp[i].right)
		dp[i].right=solve(i,false);
	left+=dp[i].left;
	right+=dp[i].right;
	return min(left,right);

}

void read(){
//	ifstream cin("in.txt");
	int i,j,k;
	int cas;
	cin>>cas;
	while(cas--)
	{
		cin>>n>>map[0].x>>map[0].y>>maxx;
		map[0].h=map[0].y;
		map[0].y=map[0].x;
		for(i=1;i<=n;i++)
			cin>>map[i].x>>map[i].y>>map[i].h;
		qsort(map,n+1,sizeof(a),cmp);
		memset(dp,0,sizeof(dp));
		cout<<solve(0,true)<<endl;

	}
}

int main(){
	read();
	return 0;
}

posted on 2011-05-04 19:15  宇宙吾心  阅读(350)  评论(1)    收藏  举报

导航