hdu 4341 Gold miner(分组背包)

原创 2014年09月11日 17:55:45

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.

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;
}


HDU 4341 Gold miner (分组背包)

题意:一个人在原点(0,0)抓金子,每块金子有一个获得需要的时间t和价值v。而且有的金子可能在一条直线上,那只能先抓近的,再抓远的。求在给定时间T下,所能获得的最大价值。 这题可以转化为分组的...
  • u013491262
  • u013491262
  • 2014-03-12 22:45:56
  • 607

hdu 4341 Gold miner 分组背包

题意:矿工起初在(0,0)位置,有n种金矿,给出每种金矿的坐标,花费时间和价值。在同一条线上的金矿必须先抓近的再抓远的,若近的不抓没办法抓远的。要求在T时间内获得的最大价值。完全没有想到使用分组背包来...
  • Little_boy_z
  • Little_boy_z
  • 2018-02-09 18:31:41
  • 11

hdu 4341 Gold miner 分组背包

简单分组背包,同一斜率为一组。   #include #include #include #include #include using namespace std; double EP...
  • taozifish
  • taozifish
  • 2012-08-19 19:57:08
  • 285

hdu 4341 Gold miner (分组背包)

Problem Description Homelesser likes playing Gold miners in class. He has to pay much attention t...
  • hndu__lz
  • hndu__lz
  • 2017-02-06 19:34:29
  • 124

HDU 4341 Gold miner (分组背包)

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...
  • long71751380
  • long71751380
  • 2013-08-22 01:03:19
  • 1286

hdu - 4341 - Gold miner - 分组背包

题意:一个人在原点(0,0)抓金子,每块金子有一个获得需要的时间t和价值v。而且有的金子可能在一条直线上,那只能先抓近的,再抓远的。求在给定时间T下,所能获得的最大价值。 http://acm.hd...
  • Julyana_Lin
  • Julyana_Lin
  • 2012-10-31 17:13:24
  • 384

HDU 4341 Gold miner(分组背包)

HDU 4341 Gold miner(分组背包) http://acm.hdu.edu.cn/showproblem.php?pid=4341 题意: 一个人在原点(0,0)抓金子,每...
  • u013480600
  • u013480600
  • 2014-10-31 16:24:07
  • 1098

HDU 4341 Gold miner

题目链接 题意:一个人在原点(0,0)抓金子,n~200个金子,每块金子有一个获得需要的时间t和价值v。而且有的金子可能在一条直线上,那只能先抓近的,再抓远的。求在给定时间T~40000下,所能获得...
  • u013625492
  • u013625492
  • 2015-03-13 10:47:54
  • 262

HDU 4341 Gold miner 分组背包变形

题意: 挖金矿,人在(0,0)点 n个金子 游戏时间为T 下面n行 (x, y) cost val 且若人 和 多块金子共线时,只能先取最近的金子,依次取,就是和游戏一样的。 且所有点只在...
  • qq574857122
  • qq574857122
  • 2014-10-10 13:42:37
  • 821

HDU 4341 Gold miner(DP 分组背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4341 明显的分组背包问题,由于排序考虑不周全导致wa数次 分组按照共线的分组!每组只能选择一个! ...
  • sunrainchy
  • sunrainchy
  • 2013-09-20 16:11:14
  • 494
收藏助手
不良信息举报
您举报文章:hdu 4341 Gold miner(分组背包)
举报原因:
原因补充:

(最多只允许输入30个字)