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

HDU4341:Gold miner(分组背包)

2017-02-01 00:50 316 查看


Gold miner

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2240    Accepted Submission(s): 855


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

 

Source

2012 Multi-University Training Contest 5

题意:玩家在(0,0)位置,给N块金块的横纵坐标,消耗时间,价值,求在t时间内能得到的最大值,其中在一条直线上的金块需要将前面的挖走才能挖后面的。 

思路:将斜率相同的金块归为一类,将同一类前面的金块的价值和耗时依次加到后面的金块上,然后用分组背包解法即可,即每类取一个金块,分组背包dp[i][j]表示前i组物品在j容量内得到的最大值,dp[i][j] = max(dp[i-1][j], dp[i-1][j-c[k]] + v[k]),其中c为代价,v为价值,k为第i组的物品,该方程降维处理代码如下

# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
struct node
{
int x, y, t, v;
}a[205];
int b[205][205], dp[40010];
int cmp(node a, node b)
{
if(a.x*b.y != a.y*b.x)
return a.y*b.x < b.y*a.x;
else
return a.y < b.y;
}
int main()
{
int n, t, cas=1;
while(~scanf("%d%d",&n,&t))
{
memset(dp, 0, sizeof(dp));
memset(b, 0, sizeof(b));
for(int i=0; i<n; ++i)
scanf("%d%d%d%d",&a[i].x, &a[i].y, &a[i].t, &a[i].v);
sort(a, a+n, cmp);
int cnt=0;
for(int i=0; i<n; ++i)
{
b[cnt][++b[cnt][0]] = i;
if(a[i].x*a[i+1].y == a[i].y*a[i+1].x)
{
a[i+1].t += a[i].t;
a[i+1].v += a[i].v;
if(i == n-1)
++cnt;
}
else
++cnt;
}
for(int i=0; i<cnt; ++i)
for(int j=t; j>=0; --j)
for(int k=1; k<=b[i][0]; ++k)
if(j>=a[b[i][k]].t)
dp[j] = max(dp[j], dp[j-a[b[i][k]].t] + a[b[i][k]].v);
printf("Case %d: %d\n",cas++,dp[t]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: