hdu acm 4341 Gold miner

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值