poj3257(Cow Roller Coaster)DP

题意:要连出一个从1-L的过山车线,给出n段可选的建设方案。每段都有起始位置,终止位置,代价,和乐趣程度。要实现1-L的长度中,相邻两端要首尾相连,总建设代价控制在B之内,问最多能获得多少乐趣程度。


解法:二维dp, num[i][j]记录恰好建设到i并且用掉代价j多能获得的最多乐趣。先将每段可选方案按照位置排序,然后进行转移。最后选max(num[L][i]),i from 0 to B;

代码:

/****************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>

using namespace std;

#define eps 1e-8
typedef long long LL;


struct point
{
    int s,e,fun,cost;
} points[10010];
bool operator<(point a,point b)
{
   if(a.s!=b.s)
        return a.s<b.s;
   return a.e<b.e;
}
int L,n,b;
LL num[1100][1010];

int main()
{
 scanf("%d%d%d",&L,&n,&b);
 memset(num,-1,sizeof num);
 for(int i=0;i<n;i++)
 {
     scanf("%d%d%d%d",&points[i].s,&points[i].e,&points[i].fun,&points[i].cost);
     points[i].e+=points[i].s;
     points[i].s++;
 }
 num[0][0]=0;
 sort(points,points+n);
 for(int i=0;i<n;i++)
 {
     for(int j=0;j<=b;j++)
     {
         if(num[points[i].s-1][j]!=-1)
         {
             if(j+points[i].cost>b)
                continue;
                LL &tool=num[points[i].e][j+points[i].cost];
                 tool=max(tool,num[points[i].s-1][j]+points[i].fun);
         }
     }
 }
 LL ans=-1;
 for(int i=0;i<=b;i++)
     ans=max(ans,num[L][i]);
   cout<<ans<<endl;
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值