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 需要处理的分组背包 蛮有意义的题目

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...

HDU 4341 Gold miner(分组背包)

HDU 4341 Gold miner(分组背包) http://acm.hdu.edu.cn/showproblem.php?pid=4341 题意: 一个人在原点(0,0)抓金子,每...

hdu4341 Gold miner 分组背包dp

题意: 思路:

HDU4341——Gold miner(分组背包)

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...

hdu 4341 Gold miner(分组01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=4341 看到这个图好亲切,黄金矿工,很好玩的游戏。。。 题意:矿工起初在(0,0)位置,有n种金矿,给出每种金...

hdu acm 4341 Gold miner

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(...

hdu 4341 Gold miner

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4341 //author:zhengjj.asd@gmail.com //要取ai位置的黄金的话,必须把斜...

Hdu 4341 Gold miner

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...

hdu4341(分组背包)

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...

几个分组背包题 zoj 3450 hdu 4341 hdu 4345

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3450 http://acm.hdu.edu.cn/showproblem...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 4341 Gold miner(分组背包)
举报原因:
原因补充:

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