poj 3257 二维dp

Cow Roller Coaster
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2837 Accepted: 1121

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 ≤ - 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:  LN and  B
Lines 2.. N+1: Line  i+1 contains four space-separated integers, respectively:  XiWiFi, 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.





题意:

是给定长度L的路径,N 行数据,和所能承载的金额

n行数据中:给出起始点和长度(就是给出了终点),fun值,和使用此部分消耗的金额值

求解:

在所能承受的金额的情况下求最大的fun值


题解:

0 1 背包问题,放与不放的问题

dp[a[i].x+a[i].w][j+a[i].c]=max(dp[a[i].x+a[i].w][j+a[i].c],dp[a[i].x][j]+a[i].f);

因为总长度和总金额不会变,因此用

dp【】【】     第一维作为长度记载,第二维作为消费记载

每个元素的值记录fun值


因此上叙状态转移方程式表示:

使用某一个部件的fun值等于使用这个部件,和不适用这个部件的最大值


注意:

这里一定要先对 起始点 x  进行从小到大排序

可以思考一下为什么不对终点进行排序



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int L,n,b;
int dp[1010][1010];

struct component
{
    int x,w,f,c;
}a[10010];

int cmp(const component k1,const component k2)
{
    return k1.x<k2.x;
}

void init()
{
    for(int i=0;i<n;i++)
        scanf("%d%d%d%d",&a[i].x,&a[i].w,&a[i].f,&a[i].c);
    sort(a,a+n,cmp);
}

void deal()
{
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<=b-a[i].c;j++){
            if(dp[a[i].x][j]!=-1){//是否已经走过
                int t=max(dp[a[i].x+a[i].w][j+a[i].c],dp[a[i].x][j]+a[i].f);
                dp[a[i].x+a[i].w][j+a[i].c]=t;
            }
        }
    }

//-----------------寻找最大fun值-----------------//
    int temp=-1;
    for(int i=0;i<=b;i++)
            temp=max(temp,dp[L][i]);
    printf("%d\n",temp);
}

int main()
{
    scanf("%d%d%d",&L,&n,&b);
    init();
    deal();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值