1649: [Usaco2006 Dec]Cow Roller Coaster

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点向后遍历。。。
后来看到hzwer大神的代码,才发现直接DP即可f[i][j]表示从0到j,用了成本为i得到的最大的快乐指数。。。
然后先按开始的点从小到大加入。。。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<map>
 6 #include<queue>
 7 #include<cstring>
 8 #define ll long long
 9 #define maxn 1010
10 #define maxm 10010
11 #define inf 100000000
12 using namespace std;
13 struct steel{
14     int x,w,f,c;
15 }a[maxm];
16 ll f[maxn][maxn];
17 bool cmp(steel a,steel b){
18     return a.x<b.x;
19 }
20 int main(){
21     int n,m,l;
22     memset(f,-1,sizeof(f));
23     f[0][0]=0;
24     scanf("%d%d%d",&n,&m,&l);
25     for(int i=1;i<=m;i++)scanf("%d%d%d%d",&a[i].x,&a[i].w,&a[i].f,&a[i].c);
26     sort(a+1,a+m+1,cmp);
27     for(int i=1;i<=m;i++)
28     {
29         int ed=a[i].x+a[i].w;
30         for(int j=a[i].c;j<=l;j++)
31             if(f[j-a[i].c][a[i].x]!=-1)f[j][ed]=max(f[j][ed],f[j-a[i].c][a[i].x]+a[i].f);
32     }
33     ll ans=-1;
34     for(int i=0;i<=l;i++)
35         ans=max(ans,f[i][n]);
36     printf("%lld\n",ans);
37 }
View Code

 

posted @ 2015-10-12 15:07  HTWX  阅读(99)  评论(0编辑  收藏  举报