HDU4341:Gold miner(分组背包)

本文介绍了一个经典游戏《挖金矿》的问题解决方法。游戏中玩家需在限定时间内收集价值最大的金块组合,尤其关注同一直线上的金块获取顺序。文章通过分组背包算法优化解决方案,并提供了详细的实现代码。

Gold miner

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2240    Accepted Submission(s): 855


Problem Description
Homelesser likes playing Gold miners in class. He has to pay much attention to the teacher to avoid being noticed. So he always lose the game. After losing many times, he wants your help.

To make it easy, the gold becomes a point (with the area of 0). You are given each gold's position, the time spent to get this gold, and the value of this gold. Maybe some pieces of gold are co-line, you can only get these pieces in order. You can assume it can turn to any direction immediately.
Please help Homelesser get the maximum value.
 

Input
There are multiple cases.
In each case, the first line contains two integers N (the number of pieces of gold), T (the total time). (0<N≤200, 0≤T≤40000)
In each of the next N lines, there four integers x, y (the position of the gold), t (the time to get this gold), v (the value of this gold). (0≤|x|≤200, 0<y≤200,0<t≤200, 0≤v≤200)
 

Output
Print the case number and the maximum value for each test case.
 

Sample Input
  
3 10 1 1 1 1 2 2 2 2 1 3 15 9 3 10 1 1 13 1 2 2 2 2 1 3 4 7
 

Sample Output
  
Case 1: 3 Case 2: 7
 

Author
HIT
 

Source


题意:玩家在(0,0)位置,给N块金块的横纵坐标,消耗时间,价值,求在t时间内能得到的最大值,其中在一条直线上的金块需要将前面的挖走才能挖后面的。 
思路:将斜率相同的金块归为一类,将同一类前面的金块的价值和耗时依次加到后面的金块上,然后用分组背包解法即可,即每类取一个金块,分组背包dp[i][j]表示前i组物品在j容量内得到的最大值,dp[i][j] = max(dp[i-1][j], dp[i-1][j-c[k]] + v[k]),其中c为代价,v为价值,k为第i组的物品,该方程降维处理代码如下
# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
struct node
{
    int x, y, t, v;
}a[205];
int b[205][205], dp[40010];
int cmp(node a, node b)
{
    if(a.x*b.y != a.y*b.x)
        return a.y*b.x < b.y*a.x;
    else
        return a.y < b.y;
}
int main()
{
    int n, t, cas=1;
    while(~scanf("%d%d",&n,&t))
    {
        memset(dp, 0, sizeof(dp));
        memset(b, 0, sizeof(b));
        for(int i=0; i<n; ++i)
            scanf("%d%d%d%d",&a[i].x, &a[i].y, &a[i].t, &a[i].v);
        sort(a, a+n, cmp);
        int cnt=0;
        for(int i=0; i<n; ++i)
        {
            b[cnt][++b[cnt][0]] = i;
            if(a[i].x*a[i+1].y == a[i].y*a[i+1].x)
            {
                a[i+1].t += a[i].t;
                a[i+1].v += a[i].v;
                if(i == n-1)
                    ++cnt;
            }
            else
                ++cnt;
        }
        for(int i=0; i<cnt; ++i)
            for(int j=t; j>=0; --j)
                for(int k=1; k<=b[i][0]; ++k)
                    if(j>=a[b[i][k]].t)
                        dp[j] = max(dp[j], dp[j-a[b[i][k]].t] + a[b[i][k]].v);
        printf("Case %d: %d\n",cas++,dp[t]);
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值