关闭

hdu acm 4341 Gold miner

标签: 分组背包
280人阅读 评论(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;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Ningbo [1217] Dinner(简单题,但是注意输出,pe3遍)

1、http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1217 注意输出,中间有空格,最后没有,处理空格错了3遍 2、题目: [121...
  • sdjzping
  • sdjzping
  • 2014-02-15 10:20
  • 1051

[ACM] hdu 龟兔赛跑

龟兔赛跑 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su...
  • sr19930829
  • sr19930829
  • 2014-01-26 22:10
  • 2605

poj/hdu/zoj矩阵连乘题集+题解代码 矩阵28练

你能全都做完,你的矩阵就小成了, 1.hdu 1005 矩阵基础题  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题解代码链接...
  • a601025382s
  • a601025382s
  • 2013-08-24 10:46
  • 2666

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
  • 514

hdu 4341 Gold miner 需要处理的分组背包 蛮有意义的题目

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...
  • hnust_xiehonghao
  • hnust_xiehonghao
  • 2012-08-10 23:29
  • 831

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
  • 1075

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
  • 334

HDU4341——Gold miner(分组背包)

Gold miner Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...
  • say_c_box
  • say_c_box
  • 2017-03-23 10:18
  • 290

hdu4341 Gold miner 分组背包dp

题意: 思路:
  • u011645923
  • u011645923
  • 2014-11-09 16:26
  • 508

hdu 4341 Gold miner(分组01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=4341 看到这个图好亲切,黄金矿工,很好玩的游戏。。。 题意:矿工起初在(0,0)位置,有n种金矿,给出每种金...
  • u013081425
  • u013081425
  • 2014-03-07 20:24
  • 1069
    个人资料
    • 访问:51136次
    • 积分:1874
    • 等级:
    • 排名:千里之外
    • 原创:135篇
    • 转载:4篇
    • 译文:0篇
    • 评论:15条
    最新评论