1. 程式人生 > >【題解】P2854 [USACO06DEC]牛的過山車Cow Roller Coaster

【題解】P2854 [USACO06DEC]牛的過山車Cow Roller Coaster

edi hcm c-c 排序 pan int eight ssi lrm

P2854 [USACO06DEC]牛的過山車Cow Roller Coaster

題目描述

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.

奶牛們正打算造一條過山車軌道.她們希望你幫忙,找出最有趣,但又符合預算 的方案. 過山車的軌道由若幹鋼軌首尾相連,由x=0處一直延伸到X=L(1≤L≤1000)處.現有N(1≤N≤10000)根鋼軌,每根鋼軌的起點 Xi(0≤Xi≤L- Wi),長度wi(l≤Wi≤L),有趣指數Fi(1≤Fi≤1000000),成本Ci(l≤Ci≤1000)均己知.請確定一 種最優方案,使得選用的鋼軌的有趣指數之和最大,同時成本之和不超過B(1≤B≤1000).

輸入輸出格式

輸入格式:

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 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.

輸入輸出樣例

輸入樣例#1: 復制

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

輸出樣例#1: 復制

17

說明

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.

思路

  • 本題與01背包相似
  • 但不同的是添加了對長度與位置的限制
  • 那麽只要在一維的01背包基礎上增加一維表示位置即可
  • $f[k,j]$表示使用j錢鋪到k的最優值,$g[]$表示歡樂值,$w[]$表示長度,$c[]$表示費用

$$if(x[i]+w[i]=k) f[k,j]=max(f[k-w[i],j-c[i]]+g[i],f[k,j])$$

  • 但這樣超時,只有x[i]+w[i]=k時才需要考慮轉移,那麽我們就可以將方程化為

$$f[v][j]=max(f[u][j-c[i]]+g[i],f[v][j]) v=u+w[i]$$

  • 但這樣就要對數據進行排序才能解決無後效性的問題

代碼

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 1324567
using namespace std;
ll l,n,b,ans,f[1002][1002];
struct po{
    ll x,w,f,c;
}a[10009];
bool cmp(po xx,po yy){return xx.x<yy.x;} 
inline int read(){
    int x=0,w=1;
    char ch=getchar();
    while((ch<‘0‘||ch>‘9‘)&&ch!=‘-‘) ch=getchar();
    if(ch==‘-‘) w=-1,ch=getchar();
    while(ch>=‘0‘&&ch<=‘9‘) x=(x<<3)+(x<<1)+ch-48,ch=getchar();
    return x*w;
}
int main()
{
    freopen("p2854.in","r",stdin);
    freopen("p2854.out","w",stdout);
    l=read(),n=read(),b=read();
    for(ll i=1;i<=n;i++) a[i].x=read(),a[i].w=read(),a[i].f=read(),a[i].c=read();  
    sort(a+1,a+n+1,cmp);
    memset(f,-1,sizeof(f));
    ans=-inf;
    f[0][0]=0;
    for(ll i=1;i<=n;i++) {
         ll u=a[i].x;
         ll v=a[i].x+a[i].w;
         for(ll j=b;j>=a[i].c;j--)
              if(f[u][j-a[i].c]!=(-1)) f[v][j]=max(f[u][j-a[i].c]+a[i].f,f[v][j]);   //f[u,j]表示用j的錢鋪到u的最優值
    }
    for(ll i=0;i<=b;i++)
    ans=max(ans,f[l][i]);
    printf("%lld\n",ans);
    return 0;
}
?

【題解】P2854 [USACO06DEC]牛的過山車Cow Roller Coaster