hdu4341(分组背包)

Gold miner

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

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

 解析:我们可以把在同一直线上的金矿假设为同一组,然后从第二个开始,也就是在一条直线上的次远离原点的金矿,它的价值和捕获时间就要加上在它之前的金矿,往下也是如此,这时候,就是可以变成分组背包问题。

#include<bits/stdc++.h>
using namespace std;

#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int maxn=220;
int N,T;
int dp[50000];
struct node{
	int x,y,t,v;
}p[maxn];

int cmp(node a,node b)//斜率从小到大排序,相同则靠近原点的先排 
{
	if(a.x*b.y==a.y*b.x)return a.y<b.y;
	return a.x*b.y>b.x*a.y;
}
int main()
{
	while(~scanf("%d%d",&N,&T))
	{
		for(int i=0; i<N; i++)
		{
			scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].t,&p[i].v);
		}
		sort(p,p+N,cmp);
		vector<node> ve[maxn];
		
		ve[0].push_back(p[0]);
		int cnt=0;
		node now;
		for(int i=1; i<N; i++)
		{
			int len=ve[cnt].size();
			if(ve[cnt][len-1].x*p[i].y==ve[cnt][len-1].y*p[i].x)
			{
				now.x=p[i].x;now.y=p[i].y;
				now.t=ve[cnt][len-1].t+p[i].t;now.v=ve[cnt][len-1].v+p[i].v;
				ve[cnt].push_back(now);
			}
			else 
			{
				cnt++;
				now.x=p[i].x;now.y=p[i].y;
				now.t=p[i].t;now.v=p[i].v;
				ve[cnt].push_back(now);
			}
		}
		mem(dp,0);
		
		//分组背包模板 
        for(int i=0;i<=cnt;i++)
            for(int j=T;j>=ve[i][0].t;j--)
                for(int k=0;k<ve[i].size();k++)
            		dp[j]=max(dp[j],dp[j-ve[i][k].t]+ve[i][k].v);
        
        printf("Case %d: %d\n",cas++,dp[T]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yu121380/article/details/81408594