bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

来源:互联网 发布:知乎rss 编辑:程序博客网 时间:2024/05/03 18:35

Description

The cows are building a roller coaster! They want your help to design as fun a roller coaster as possible, while keeping to the budget. The roller coaster will be built on a long linear stretch of land of length L (1 <= L <= 1,000). The roller coaster comprises a collection of some of the N (1 <= N <= 10,000) different interchangable components. Each component i has a fixed length Wi (1 <= Wi <= L). Due to varying terrain, each component i can be only built starting at location Xi (0 <= Xi <= L-Wi). The cows want to string together various roller coaster components starting at 0 and ending at L so that the end of each component (except the last) is the start of the next component. Each component i has a "fun rating" Fi (1 <= Fi <= 1,000,000) and a cost Ci (1 <= Ci <= 1000). The total fun of the roller coster is the sum of the fun from each component used; the total cost is likewise the sum of the costs of each component used. The cows' total budget is B (1 <= B <= 1000). Help the cows determine the most fun roller coaster that they can build with their budget.

奶牛们正打算造一条过山车轨道.她们希望你帮忙,找出最有趣,但又符合预算的方案.  过山车的轨道由若干钢轨首尾相连,由x=0处一直延伸到X=L(1≤L≤1000)处.现有N(1≤N≤10000)根钢轨,每根钢轨的起点Xi(0≤Xi≤L- Wi),长度wi(l≤Wi≤L),有趣指数Fi(1≤Fi≤1000000),成本Ci(l≤Ci≤1000)均己知.请确定一种最优方案,使得选用的钢轨的有趣指数之和最大,同时成本之和不超过B(1≤B≤1000).

Input

* Line 1: Three space-separated integers: L, N and B.

 * Lines 2..N+1: Line i+1 contains four space-separated integers, respectively: Xi, Wi, Fi, and Ci.

    第1行输入L,N,B,接下来N行,每行四个整数Xi,wi,Fi,Ci.

Output

* Line 1: A single integer that is the maximum fun value that a roller-coaster can have while staying within the budget and meeting all the other constraints. If it is not possible to build a roller-coaster within budget, output -1.

Sample Input

5 6 10
0 2 20 6
2 3 5 6
0 1 2 1
1 1 1 3
1 2 5 4
3 2 10 2


Sample Output

17
选用第3条,第5条和第6条钢轨

题意是有一段0到L的区间,要求用一些线段覆盖,每一条线段都有价值和代价,求在将区间完全覆盖(不能重叠)和总代价不超过B的条件下能获得的最大价值

首先很容易想到一种背包dp的方法

先把线段排序,不用说

f[i][j][k]表示前i个线段覆盖0到j的区间代价为k的最大价值

这样100e肯定TLE+MLE,不用说

然后不会优化,去orz了黄巨大才会做

把第一维省掉,但是依然TLE

但是我们发现枚举第i段线段,那么它能更新的只有几个状态

学着黄巨大把方程倒一下,令f[i][j]表示费用为i,能覆盖0到j的最大价值

那么枚举费用k,因为不能重叠,只会更新从当前线段左端点开始的方案

这样只要O(NB)就可以了

#include<cstdio>#include<cstring>#include<algorithm>#define LL long longusing namespace std;struct work{int l,r,f,c;}a[10010];int n,m,b;LL f[1010][1010],ans=-1;inline bool cmp(const work &a,const work &b){return a.l<b.l||a.l==b.l&&a.r<b.r;}inline int max(int a,int b){return a>b?a:b;}inline int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int main(){memset(f,-1,sizeof(f));f[0][0]=0;n=read();m=read();b=read(); for (int i=1;i<=m;i++)  {  a[i].l=read();  a[i].r=a[i].l+read();  a[i].f=read();  a[i].c=read();  }sort(a+1,a+m+1,cmp);for (int i=1;i<=m;i++)  for (int j=a[i].c;j<=b;j++)    if (f[j-a[i].c][a[i].l]!=-1)      f[j][a[i].r]=max(f[j][a[i].r],f[j-a[i].c][a[i].l]+a[i].f);for (int i=0;i<=b;i++)ans=max(ans,f[i][n]);printf("%lld\n",ans);}


0 0
原创粉丝点击