hdu 4341 分组背包

20 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=4341



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
/**
hdu 4341 分组背包
题目大意: 一个人在原点(0,0)抓金子,每块金子有一个价值v和获得需要的时间t。
           如果金子在一条直线上,那只能先抓近的,再抓远的。求在给定时间T下,所能获得的最大价值。
解题思路:分组背包问题。把在同一直线上的点划分为同一个组,因为分组背包问题每个组只能选一件或不选,
           所以要对原先的每个点进行处理,把同一组中,后面的点的t和v的值等于它前面所有点的和 这样,
           选了它就一定选择了它前面的点了 剩下就是分组背包的问题了
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=205;

struct note
{
    int x,y,t,v;
    bool operator <(const note &other)const
    {
        if(x*other.y!=other.x*y)
            return x*other.y<y*other.x;
        return y<other.y;
    }
}node[maxn];

int n,T;
int dp[maxn*maxn],num[maxn][maxn];

int main()
{
    int tt=0;
    while(~scanf("%d%d",&n,&T))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&node[i].x,&node[i].y,&node[i].t,&node[i].v);
        }
        sort(node,node+n);
        int cnt=0;
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)///斜率相同划分为同一组
        {
            num[cnt][++num[cnt][0]]=i;
            if(node[i].x*node[i+1].y==node[i].y*node[i+1].x)
            {
                node[i+1].t+=node[i].t;
                node[i+1].v+=node[i].v;
                if(i==n-1)
                    cnt++;
            }
            else
                cnt++;
        }
        memset(dp,0,sizeof(dp));///分组背包
        for(int i=0;i<cnt;i++)
        {
            for(int j=T;j>=0;j--)
            {
                for(int k=1;k<=num[i][0];k++)
                {
                    int u=num[i][k];
                    if(j-node[u].t>=0)
                        dp[j]=max(dp[j],dp[j-node[u].t]+node[u].v);
                }
            }
        }
        printf("Case %d: %d\n",++tt,dp[T]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值