HDU 4341 分组背包

本文解析了一款简化版黄金矿工游戏的算法实现过程,采用分组背包思想解决同一直线上金子的获取顺序问题,确保玩家能在限定时间内获得最大价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Gold miner
Time Limit:2000MS     
Memory Limit:32768KB    

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
 
题意:
   黄金矿工的游戏
   同一直线上的金子,只能先抓近的
题解:分组背包
///1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//***************************************************************

struct ss
{
    int x,y,v,t;
}p[2050];
vector<ss >belong[2050];
bool cmp(ss a,ss b)
{
    if(a.y*b.x!=a.x*b.y)
        return a.y*b.x<a.x*b.y;
    else return a.y<b.y;
}
int dp[40005];
int main()
{

    int oo=1;
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)belong[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].t,&p[i].v);
        }
        p[0].x=-1;
        p[0].y=-1;
        sort(p+1,p+n+1,cmp);
        int zu=0;
        for(int i=1;i<=n;i++)
        {
            if(i!=1&&p[i].y*p[i-1].x==p[i].x*p[i-1].y)
            {
                ss kk;
                kk.v=belong[zu][belong[zu].size()-1].v+p[i].v;
                kk.t=belong[zu][belong[zu].size()-1].t+p[i].t;
                belong[zu].push_back(kk);
            }
            else {
                ss kk;
                kk.v=p[i].v;
                kk.t=p[i].t;
                belong[++zu].push_back(kk);
            }

        }
        //cout<<3213121<<endl;
        for(int i=1;i<=zu;i++)
        {
            for(int j=m;j>=0;j--)
            {
                for(int k=0;k<belong[i].size();k++)
                {
                    if(j>=belong[i][k].t)
                    {
                        dp[j]=max(dp[j],dp[j-belong[i][k].t]+belong[i][k].v);
                    }
                }
            }
        }
        printf("Case %d: %d\n",oo++,dp[m]);

    }

    return 0;
}
代码狗

 

 

转载于:https://www.cnblogs.com/zxhl/p/4746136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值