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.
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.
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
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
选用第3条,第5条和第6条钢轨
开始想的是每个铁轨看做一条边,然后从0点向后遍历。。。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }