poj 3257 Cow Roller Coaster(二维背包)

本文介绍了一种过山车设计问题,目标是在限定预算内通过选择合适的组件来最大化过山车的整体乐趣值。文章详细解释了如何使用动态规划解决此问题,并提供了完整的AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cow Roller Coaster
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2575 Accepted: 1000

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 theN (1 ≤N ≤ 10,000) different interchangable components. Each componenti has a fixed lengthWi (1 ≤ WiL). Due to varying terrain, each componenti can be only built starting at locationXi (0 ≤ XiL - Wi). The cows want to string together various roller coaster components starting at 0 and ending atL 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 costCi (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 isB (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 andB.
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

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

Hint

Taking the 3rd, 5th and 6th components gives a connected roller-coaster with fun value 17 and cost 7. Taking the first two components would give a more fun roller-coaster (25) but would be over budget.

题意:要建造一座过山车,给出建造需要的零件。每个零件都有一个固定的起始位置、长度、乐趣值和花费。给出过山车的长度,零件的数量,总共的预算,要求出在预算范围内选出一些零件,使总的乐趣值最大。
分析:二维背包问题。设dp[i][j]表示在i的位置修建零件花费为j时所获得的最大乐趣值。转移方程为dp[com[i].x+com[i].w][j+com[i].c]>?=dp[com[i].x][j]+com[i].f

AC代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>

using namespace std;
struct node
{
    int x,w,f,c;
}com[10005];
int L,N,B;
int dp[1005][1005];
bool cmp(node a,node b)
{
    return a.x+a.w<b.x+b.w;
}
int main()
{
    while(cin>>L>>N>>B)
    {
        for(int i=0;i<N;i++)
        cin>>com[i].x>>com[i].w>>com[i].f>>com[i].c;
        sort(com,com+N,cmp);
        memset(dp,-1,sizeof(dp));   
        for(int i=0;i<=B;i++) dp[0][i]=0;
        for(int i=0;i<N;i++)
        for(int j=0;j<=B-com[i].c;j++)
        {
            if(dp[com[i].x][j]<0) continue;     //小于0时没法到达
            dp[com[i].x+com[i].w][j+com[i].c]=max(dp[com[i].x+com[i].w][j+com[i].c],dp[com[i].x][j]+com[i].f);
        }
        cout<<dp[L][B]<<endl;
    }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值