您的位置:首页 > 编程语言 > Go语言

hdu 4341 Gold miner (分组背包)

2017-02-06 19:34 459 查看
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

 

Author

HIT

 
题意:一个人在原点(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<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int cnt;       //组数
const int N=205;
const int M=40005;
struct node{
int x,y,t,v;
}str
;
int group

;
int dp[M];
bool cmp(node aa,node bb)
{
if(aa.x*bb.y==aa.y*bb.x)
return aa.y<bb.y;    //斜率相同只能按Y排序,因为x已知条件可能为0;
return aa.y*bb.x<aa.x*bb.y;
}
int solve(int t)
{
memset(dp,0,sizeof(dp));
for(int i=0;i<=cnt;i++)
for(int k=t;k>=0;k--)
for(int j=1;j<=group[i][0];j++)
if(k-str[group[i][j]].t>=0)
dp[k]=max(dp[k],dp[k-str[group[i][j]].t]+str[group[i][j]].v);
return dp[t];
}
int main()
{
int n,t;
int T=1;
while(~scanf("%d%d",&n,&t))
{
for(int i=0;i<n;i++)
scanf("%d%d%d%d",&str[i].x,&str[i].y,&str[i].t,&str[i].v);
sort(str,str+n,cmp);
cnt=-1;
for(int i=0;i<n;i++)
{
cnt++;
group[cnt][0]=0;
group[cnt][++group[cnt][0]]=i;
int x1=str[i].x;
int y1=str[i].y;
int t=str[i].t;
int v=str[i].v;
for(i++;i<n;i++)
{
int x2=str[i].x;
int y2=str[i].y;
t+=str[i].t;
v+=str[i].v;
if(x1*y2==x2*y1)
{
str[i].t=t;
str[i].v=v;
group[cnt][++group[cnt][0]]=i;
}
else
{
i--;
break;
}
}
}
/*for(int i=0;i<=cnt;i++)
for(int j=1;j<=group[i][0];j++)
printf("%d %d\n",i,str[group[i][j]].t);*/
printf("Case %d: ",T++);
printf("%d\n",solve(t));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: