区间覆盖最小代价
由于N<=10000
排序后想到N*N的遍历
不断更新add[i](表示到i最小代价)里的值,使之最小
View Code
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxint=999999999;
struct data
{
int ll,rr,c;
}s[10009];
int add[10009];
int cmp(data a,data b)
{
if(a.ll==b.ll)
return a.rr<b.rr;
else
return a.ll<b.ll;
}
int main()
{
int n,begin,end;
while(scanf("%d%d%d",&n,&begin,&end)!=EOF)
{
int i,j;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&s[i].ll,&s[i].rr,&s[i].c);
add[i]=maxint;
s[i].rr++;
}
sort(&s[1],&s[n+1],cmp);
if(s[1].ll>begin)
{
printf("-1\n");continue;
}
int mmax=s[1].rr;
for(i=1;i<n;i++)
{
if(mmax>=s[i+1].ll)
{
mmax=max(mmax,s[i+1].rr);
}
else
{
break;
}
}
if(mmax-1<end)
{
printf("-1\n");continue;
}
add[1]=s[1].c;
for(i=1;i<=n;i++)
{
if(s[i].ll<=begin)
add[i]=s[i].c;
for(j=i+1;j<=n;j++)
{
if(s[i].ll==s[j].ll)continue;
if(s[i].rr>=s[j].rr)continue;
if(s[i].rr<s[j].ll)break;
add[j]=min(add[j],add[i]+s[j].c);
}
}
int mmin=maxint;
for(i=1;i<=n;i++)
{
if(s[i].rr-1>=end)
mmin=min(mmin,add[i]);
}
printf("%d\n",mmin);
}
}