hdu 4341 Gold miner(分组背包)

Gold miner

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


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 Gold miner(分组背包)_第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
 

Author
HIT
 


分组背包,主要是在一条线上时的分组,先按斜率排序,斜率相等的分一组,斜率相等的判断,x1/y1=x2/y2,可以转化为x1*y2=x2*y1,同理,斜率的大小也可以这样判

代码:

//515ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=40000+1000;
int dp[maxn];
struct node
{
    int x;
    int y;
    int t;
    int v;
}edge[maxn];
vector<int> group[250];
bool cmp(node a,node b)//按斜率排序,斜率相等时按距离排序
{
    if(a.x*b.y==a.y*b.x)
    return a.x*a.x<b.x*b.x;
    return a.x*b.y<a.y*b.x;
}
int main()
{
    int ca=1;
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&edge[i].x,&edge[i].y,&edge[i].t,&edge[i].v);
        }
        sort(edge,edge+n,cmp);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<250;i++)
        group[i].clear();
        int te=0;
        group[0].push_back(0);
        for(int i=1;i<n;i++)//将斜率相等的分到一组
        {
           if(edge[i].x*edge[i-1].y==edge[i].y*edge[i-1].x)
           group[te].push_back(i);
           else
           {
               te=te+1;
               group[te].push_back(i);
           }
        }
        for(int i=0;i<=te;i++)
        {
            for(int j=m;j>=0;j--)
            {
                int t=0;
                int v=0;
                for(int k=0;k<group[i].size();k++)
                {
                    int u=group[i][k];
                    t+=edge[u].t;
                    v+=edge[u].v;
                    if(j>=t)
                    dp[j]=max(dp[j],dp[j-t]+v);
                }
            }
        }
        printf("Case %d: ",ca++);
        printf("%d\n",dp[m]);
    }
    return 0;
}


你可能感兴趣的:(Algorithm)