POJ - 3257 Cow Roller Coaster (背包)

题目大意:要用N种材料建一条长为L的路,现在给出每种材料的长度w,起始地点x,发费c和耐久度f

问:在预算为B的情况下,建好这条路的最大耐久度是多少

解题思路:背包问题

dp[i][j]表示起始地点为i,发费为j的最大耐久度

可得转移方程

dp[i + w][j + c] = max(dp[i + w][j + c],dp[i][j] + f)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxl 1010
#define maxn 10010
#define INF 0x3f3f3f3f
int L, N, B;
int dp[maxl][maxl];
struct component {
    int x, w, f, c;
}com[maxn];

int cmp(const component a, const component b) {
    return a.x < b.x;
}

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

void solve() {
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j <= B - com[i].c; j++)
            if(dp[com[i].x][j] != -1) {
                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) ;
            }
    }

    int ans = -1;
    for(int i = 0; i <= B; i++)
        if(dp[L][i] != INF)
            ans = max(ans, dp[L][i]);
    printf("%d\n", ans);
}

int main() {
    while(scanf("%d%d%d", &L, &N, &B) != EOF ) {
        init();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-30 20:12:14

POJ - 3257 Cow Roller Coaster (背包)的相关文章

BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

有点类似背包 , 就是那样子搞... ------------------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ;  i < n ; ++i

【题解】P2854 [USACO06DEC]牛的过山车Cow Roller Coaster

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 o

poj3257(Cow Roller Coaster)DP

迭代是一开发种技术,用来把系统功能传递到一系列的增量的完整版本,每个版本一个特定固定的时间段被开发,该时间段称之为迭代. 每个迭代的经历过程: 整个迭代过程: 图中颜色代表每次开发每项活动所占的比重不同 迭代式开发的优点: 1.降低风险 2.得到早期用户反馈 3.持续测试和集成 4.适应变更 开发特征: 1.在进行大规模的投资前,就解决了关键的风险问题 2.使的早期用户反馈在初始迭代中就能出现 3.连续进行测试和集成. 4.各个目标里程碑提供了短期的焦点. 5.对过程的测量是通过实现的评定来进行

bzoj1649[Usaco2006 Dec]Cow Roller Coaster*

bzoj1649[Usaco2006 Dec]Cow Roller Coaster 题意: n条钢轨,第i条起点pi,长度为wi,价钱ci,有趣度fi,要求从0修到l使得总价钱不超过b的前提下有趣度和最大.n≤10000,l≤1000,b≤1000. 题解: 首先把钢轨组织成链表.接着dp:f[i][j]表示修到第i处花钱j,则f[i][j]=f[i-wk][j-ck]+fk,k为终点为i的钢轨.边界则设为f[0][j]都为0,f[i][j]都为负无穷,以保证从0开始修. 代码: 1 #incl

poj 2184 - Cow Exhibition (01背包) 解题报告

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10279   Accepted: 4016 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to

POJ 2184 Cow Exhibition (01背包)

题意:每行给出si和fi,代表牛的两个属性,然后要求选出几头牛,是的则求出总S与总F的和,注意S与F都不能为负数 析:用dp[i]来表示存放s[i]的时最大的f[i],其实就是一个01背包.只是取不取的关系.注意是有负数,所以把数组开大一点,然后s[i]的正负数, 我们取的顺序不同,正数是逆向,负数是正向,要不然可能有重复.还不知道为什么交G++就RE,交C++就能过. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo

poj 2184 Cow Exhibition 01背包变形

点击打开链接链接 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9288   Accepted: 3551 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to p

[luoguP2854] [USACO06DEC]牛的过山车Cow Roller Coaster(DP + sort)

传送门 先按照起点 sort 一遍. 这样每一个点的只由前面的点决定. f[i][j] 表示终点为 i,花费 j 的最优解 状态转移就是一个01背包. ——代码 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 6 int L, N, B; 7 int f[10001][1001]; 8 9 inline int read() 10

【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}(1<=i<=n,0<=j<=B) #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct Line{int x,l,w,c;}a[10001