hdu acm 4341 Gold miner

标签: 分组背包
313人阅读 评论(0) 收藏 举报
分类:
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Problem Description</span>
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
 

思路:把斜率相同的分在同一组,再处理一下,就可用分组背包了。

#include<iostream>
using namespace std;
#include<string.h>
#include<algorithm>
struct hyf
{
    int x,y,a,b;
    double z;
}h[205];
struct phyf
{
    int w[205],v[205];
}h1[205];
bool cmp(hyf a,hyf b)
{
    if(a.y*b.x==b.y*a.x) return a.y<b.y;//斜率相同的按y从小到大排;
    return a.y*b.x>b.y*a.x;
}
int dp[999999];
int main()
{
    int n,m,i,j,n1,k,k1,k2,num[205],f=0;
    struct hyf t,t1;
    while(cin>>n>>m)
    {  n1=1;k2=k1=0;
     memset(num,0,sizeof(num));
        for(i=0;i<n;i++)
           cin>>h[i].x>>h[i].y>>h[i].a>>h[i].b;
       sort(h,h+n,cmp);
      for(i=0;i<n-1;i++)
        if(h[i].y*h[i+1].x==h[i+1].y*h[i].x)
        {
            h[i+1].a=h[i].a+h[i+1].a;
             h[i+1].b=h[i].b+h[i+1].b;
        }//因为斜率相同的要按顺序排,所以相当于第二个时间和价值等于第一个加上第二个,依次类推,然后就可以分组背包了。
        else n1++;
        h1[0].w[0]=h[0].a;
        h1[0].v[0]=h[0].b;
        num[0]=1;
       for(i=0;i<n-1;i++)
        {
            if(h[i].y*h[i+1].x!=h[i+1].y*h[i].x)
                {k1++;k2=0;
                h1[k1].w[k2]=h[i+1].a;
                h1[k1].v[k2]=h[i+1].b;
                num[k1]++;
                }
            else
            {  
                 k2++;
                h1[k1].w[k2]=h[i+1].a;
                h1[k1].v[k2]=h[i+1].b;
                num[k1]++;
            }
        }
        cout<<"Case "<<++f<<": ";
             memset(dp,0,sizeof(dp));
              for(i=0;i<n1;i++)
                for(j=m;j>=0;j--)
                for(k=0;k<num[i];k++)
                    if(j>=h1[i].w[k])
                  dp[j]=max(dp[j],dp[j-h1[i].w[k]]+h1[i].v[k]);
                  cout<<dp[m]<<endl;
    }
    return 0;
}


查看评论

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

受教了。这个题目是本人第一次做分组背包,比较失败。连题意带条件搞错了几个地方,最终悲剧的wa了4次。 #include #include #include using namespace std; ...
  • zhuyongqingacm
  • zhuyongqingacm
  • 2013-08-15 22:14:13
  • 568

HDU 4341 - Gold miner

B - Gold miner Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit...
  • leifjacky
  • leifjacky
  • 2016-04-26 17:01:23
  • 353

Hdu 4341 Gold miner

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...
  • Magic____
  • Magic____
  • 2012-10-08 13:06:10
  • 418

hdu 4341 Gold miner

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4341 //author:zhengjj.asd@gmail.com //要取ai位置的黄金的话,必须把斜...
  • zhengjj_asd
  • zhengjj_asd
  • 2012-08-11 13:26:59
  • 352

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) Tot...
  • caduca
  • caduca
  • 2014-09-11 17:55:45
  • 537
    个人资料
    持之以恒
    等级:
    访问量: 5万+
    积分: 2013
    排名: 2万+
    最新评论