【题解】[Usaco2006 Dec]Cow Roller Coaster

01背包,难点在于多了一个维度(二维01背包)

有费用(成本这里称“奶牛币”),有价值(有趣指数这里称“fun值”),还有一个限制条件——轨道要求连续,切必须到达终点

既然有两个限制维度,那咱就开一个二维数组:F[i][j](为了避免与变量名重复,我这里使用大写F,代码里用小写f)

F[i][j]表示:从轨道的起点某起点使用轨道,花费j奶牛币,到达该轨道的终点i,可以获得的最大的fun值

于是就有方程:

F[x[i]+w[i]][j]=max(F[x[i]+w[i]][j],F[x[i]][j-c[i]]+f[i])

注意不能到达要输出-1这里直接把最大欢乐值初始设为-1

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct rail
{
    int x,w,f,c;
} r[10001];
int l,n,b,ans,f[1001][1001];
bool cmp(rail x,rail y)
{
    return x.x<y.x;
}
int main()
{
    cin>>l>>n>>b;
    for(int i=1; i<=n; i++)
    {
        cin>>r[i].x>>r[i].w>>r[i].f>>r[i].c;
    }
    sort(r+1,r+1+n,cmp);
    memset(f,-1,sizeof(f));
    f[0][0]=0,ans=-0x3f3f3f3f;
    for(int i=1; i<=n; i++)
    {
        for(int j=b; j>=r[i].c; j--)
        {
            if(f[r[i].x][j-r[i].c]!=-1)
            {
                f[r[i].x+r[i].w][j]=max(f[r[i].x+r[i].w][j],f[r[i].x][j-r[i].c]+r[i].f);
            }
        }
    }
    for(int i=0; i<=b; i++)
    {
        ans=max(ans,f[l][i]);
    }
    cout<<ans;
}

原文地址:https://www.cnblogs.com/fox-nest/p/12210677.html

时间: 2024-10-30 16:12:42

【题解】[Usaco2006 Dec]Cow Roller Coaster的相关文章

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

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

【动态规划】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

【题解】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

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 432  Solved: 270[Submit][Status] Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N <= 1,000)

[BZOJ1648][Usaco2006 Dec]Cow Picnic 奶牛野餐

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 781  Solved: 483 [Submit][Status][Discuss] Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N &

BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

直接从每个奶牛所在的farm dfs , 然后算一下.. ---------------------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<vector> #define rep( i ,

poj3257(Cow Roller Coaster)DP

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

BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N <= 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 <= M <= 10,000) one-way paths (no p