`
java-mans
  • 浏览: 11392558 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

Gold miner

Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 705Accepted Submission(s): 300


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

Source

Recommend
zhuyuanchen520
题意:一个人在原点(0,0)抓金子,每块金子有一个获得需要的时间t和价值v。而且有的金子可能在一条直线上,那只能先抓近的,再抓远的。求在给定时间T下,所能获得的最大价值。
这题可以转化为分组的背包问题。分组的背包问题详解见背包九讲。
先将所有点按照斜率排序,斜率相同按照距离排序。
然后进行分组,将斜率相同的分进同一个组。
比如有5个点1,2,3,4,5,6.
1斜率小,2,3斜率相同,4,5,6斜率相同。那分三组(1),(2,3),(4,5,6)
然后在同一个组内需要处理下。比如(2,3)是先要抓2才能抓3的。那就把2,3的t和v加起来给3。这样2,3就只能取一个了,就变成分组的背包问题了。


#include <stdio.h>
#include <algorithm>
#include<math.h>
#include <string.h>
using namespace std;
struct st
{
    int x;
    int y;
    int v;
    int w;
}no[300],u[300][300];
int   cmp(const void *a,const void *b)
{
    double k1,k2;
    k1=(double)(*(struct st *)a).x/(*(struct st *)a).y;
    k2=(double)(*(struct st *)b).x/(*(struct st *)b).y;
    if(fabs(k1-k2)>0.000001) return k1>k2?1:-1;//先把斜率 按从小到大排列
	//    if(k1==k2) return k1>k2?1:-1;//先把斜率 按从小到大排列 注意对于浮点型的二级排序要特别注意 千万不要直接比较是否相等
    else return (*(struct st *)a).y>(*(struct st *)b).y?1:-1;//从小到大
}

int shu[300];
int dp[50009];
int mm=1;
int main()
{
    int i,j,m,n;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&no[i].x,&no[i].y,&no[i].w,&no[i].v);
        }
        //sort(no,no+m,cmp);
        qsort(no,m,sizeof(no[0]),cmp);
        int cos=0;
        memset(shu,0,sizeof(shu));
		
        for(i=0;i<m;i++)
        {
            u[cos][shu[cos]].x=no[i].x;
            u[cos][shu[cos]].y=no[i].y;
            u[cos][shu[cos]].w=no[i].w;
            u[cos][shu[cos]].v=no[i].v;
            shu[cos]++;
            if(i!=m-1)
            {
                while(i!=m-1&&(double)no[i+1].x/no[i+1].y==(double)no[i].x/no[i].y)
                {
                    u[cos][shu[cos]].x=no[i+1].x;
                    u[cos][shu[cos]].y=no[i+1].y;
                    u[cos][shu[cos]].w=u[cos][shu[cos]-1].w+no[i+1].w;
                    u[cos][shu[cos]].v=u[cos][shu[cos]-1].v+no[i+1].v;
                    shu[cos]++;
                    i++;
                }
                cos++;
            }
        }
		
		
        int k;
        memset(dp,0,sizeof(dp));
        for(i=0;i<=cos;i++)
        {
            for(j=n;j>=0;j--)
            {
                for(k=0;k<shu[i];k++)
                {
					//    if(j-u[i][k].w>=0&&dp[j]<dp[j-u[i][k].w]+u[i][k].v)
                    if(j-u[i][k].w>=0)
                        dp[j]=dp[j]>dp[j-u[i][k].w]+u[i][k].v?dp[j]:dp[j-u[i][k].w]+u[i][k].v;
                }
            }
        }
        printf("Case %d: ",mm++);
        printf("%d\n",dp[n]);
    }
    return 0;
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics