关闭

hdu acm 4341 Gold miner

标签: 分组背包
215人阅读 评论(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网站的观点或立场
    个人资料
    • 访问:44823次
    • 积分:1811
    • 等级:
    • 排名:千里之外
    • 原创:135篇
    • 转载:4篇
    • 译文:0篇
    • 评论:15条
    最新评论