hdu 4341 分组背包

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.
hdu 4341 分组背包_第1张图片
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;
}

你可能感兴趣的:(hdu 4341 分组背包)