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 ≤ 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.
Input
Lines 2.. N+1: Line i+1 contains four space-separated integers, respectively: Xi, Wi, Fi, and Ci.
Output
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
题意:
是给定长度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;
}