HDU 4341 Gold miner (分组背包)

Gold miner

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


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
 


 

题意来自:http://www.cnblogs.com/kuangbin/archive/2012/08/09/2629579.html

题意:一个人在原点(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就只能取一个了,就变成分组的背包问题了。
 
import java.io.*;
import java.util.*;
public class Main{
	int N,T,x,y,t,v,size=205,MAX=40005;
	int[] number;
	boolean[] boo;
	Node[] node;
	int[] dp;
	int[][] group;
	public static void main(String args[]){
		new Main().work();
	}
	void work(){
		Scanner sc=new Scanner(new BufferedInputStream(System.in));
		int Case=1;
		while(sc.hasNext()){
			
			N=sc.nextInt();
			T=sc.nextInt();
			
			node=new Node[N];
			boo=new boolean[size];
			dp=new int[MAX];
			group=new int[size][size];
			number=new int[size];
			
			for(int i=0;i<N;i++){
				x=sc.nextInt();
				y=sc.nextInt();
				t=sc.nextInt();
				v=sc.nextInt();
				node[i]=new Node(x,y,t,v);
			}
			Arrays.sort(node);
			int index=0;
			// 背包分组
			for(int i=0;i<N;i++){
				
				if(boo[i]) continue;
				boo[i]=true;
				group[index][number[index]++]=i;
				for(int j=i+1;j<N;j++){
					
					if(!boo[j]&&node[i].x*node[j].y==node[i].y*node[j].x){
						boo[j]=true;
						group[index][number[index]++]=j;
					}
				}
				index++;
			}
			
			//更新每个分组的信息
			for(int i=0;i<index;i++){
				for(int j=1;j<number[i];j++){
					node[group[i][j]].t+=node[group[i][j-1]].t;
					node[group[i][j]].v+=node[group[i][j-1]].v;
				}
			}
			
			//分组背包
			for(int i=0;i<index;i++){
				for(int j=T;j>=0;j--){
					for(int k=0;k<number[i];k++){
						int u=group[i][k];
						if(j>=node[u].t){
							dp[j]=Math.max(dp[j],dp[j-node[u].t]+node[u].v);
						}
					}
				}
			}
			
			System.out.println("Case "+Case+++": "+dp[T]);
		}
	}
	class Node implements Comparable<Node>{
		int x;
		int y;
		int t;
		int v;
		Node(int x,int y,int t,int v){
			this.x=x;
			this.y=y;
			this.t=t;
			this.v=v;
		}
		public int compareTo(Node o) {
			if(this.x*o.y>this.y*o.x)//如果斜率不相等,按斜率大小从小到大进行排序
				return 1;
			else if(this.x*o.y==this.y*o.x&&this.y>o.y)//如果斜率相等,按它到y轴的距离从小到大进行排序
				return 1;
			return -1;
		}
	}
}



 

CCF大数据与计算智能大赛-面向电信行业存量用户的智能套餐个性化匹配模型联通赛-复赛第二名-【多分类,embedding】.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值