题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68966#problem/R
题意:给定一个区间和若干个子区间,求没有重叠区间的最大和值。
这题没啥说的,直接排序一下,就变成类似于紫书上所说的DAG上的最长路问题了。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define maxn 1010
#define INF 0x7fffffff
using namespace std;
struct node{
int st,ed,w;
bool operator <(const node&a)const{
if(st!=a.st) return st<a.st;
return ed<a.ed;
}
}A[maxn];
int dp[maxn],N,M,R,tot;
int main(){
//freopen("in.txt","r",stdin);
while(cin>>N){
cin>>M>>R;
int a,b,c;
tot=0;
for(int i=0;i<M;i++){
scanf("%d %d %d",&a,&b,&c);
A[tot++]=(node){a,b+R,c};
}
sort(A,A+tot);
int ans=0;
for(int i=0;i<tot;i++){
dp[i]=A[i].w;
for(int j=0;j<i;j++)
if(A[j].ed<=A[i].st)
dp[i]=max(dp[i],dp[j]+A[i].w);
ans=max(ans,dp[i]);
}
cout<<ans<<endl;
}
return 0;
}