BZOJ1649: [Usaco2006 dec]cow Roller Coaster Backpack DP

Source: Internet
Author: User
1649: [Usaco2006 dec]cow Roller CoasterTime Limit:5 Sec Memory limit:64 MB submit:584 solved:302 [Submit][status][discuss] Description

The cows is building a roller coaster! They want your help to design as fun a roller coaster as possible and while keeping to the budget. The roller coaster'll be built to 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 <=) different interchangable components. Each component I have a fixed length WI (1 <= wi <= L). Due to varying terrains, each component I can is 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, the end of each C Omponent (except) is the start of the next component. Each component I have 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 was 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. The cows are going to build a roller coaster track. They want you to help find the most interesting, yet budget-compliant option. The track of the    roller coaster is connected to the end of a number of rails and extends from x=0 to X=l (1≤l≤1000). Existing N (1≤n≤10000) root rails, beginning Xi (0≤XI≤L-&NBSP;WI) of each rail, length Wi (l≤wi≤l), interesting index Fi (1≤fi≤1000000), cost Ci (l≤ci≤1000) are known. Please determine an optimal scheme that makes the most interesting index of the selected rails and the sum of the costs not exceeding 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. Line 1th enters L,n,b, next N lines, four integers per line xi,wi,fi,ci. Output

* Line 1: A single integer, that's the maximum fun value, a roller-coaster can has while staying within the budget an D meeting All of 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
Select 3rd, 5th and 6th rail : first Sort by starting point, the same starting point according to the length of the rails f[i][j], the front length of the railway with J can be paved for the cost of the maximum value of the interesting index

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define LL Long long
using namespace std;
struct Node{int s,c,w,f;} A[10005];
int l,n,mx;
ll f[1005][1005];
BOOL CMP (Node A,node b)
{
	if (A.S==B.S) return a.w<b.w;
	Return a.s<b.s;
}
int main ()
{
    scanf ("%d%d%d", &l,&n,&mx);
    for (int i=1;i<=n;i++) scanf ("%d%d%d%d", &a[i].s,&a[i].w,&a[i].f,&a[i].c);
    Sort (a+1,a+n+1,cmp);
    memset (F,-1,sizeof (f));
    f[0][0]=0;
    for (int i=1;i<=n;i++)
    {
    	int e=a[i].s+a[i].w;
    	for (int j=mx;j>=a[i].c;j--)
		if (f[a[i].s][j-a[i].c]>=0)
		F[e][j]=max (f[e][j],f[a[i].s][j-a[i].c]+ A[I].F);
    }
    ll Ans=-1;
    for (int i=0;i<=mx;i++) Ans=max (Ans,f[l][i]);
    printf ("%lld\n", ans);
}
The first I position spends the maximum fun value that J can get 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.