zoukankan      html  css  js  c++  java
  • 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;
    		}
    	}
    }



     

  • 相关阅读:
    ORA-01045: user XXZY lacks CREATE SESSION privilege; logon denied
    ORA-31626:作业不存在 ORA-31633:无法创建主表"XXX.SYS_IMPORT_FULL_05"
    HTTP 错误 401.3
    mysql 简单游标
    mysql 多重游标嵌套
    表单校验 “灰白字提示”
    Eclipse连接mysql数据库出现问题
    虚拟机无法使用桥接,没有未桥接网络适配器解决办法
    每周进度条(16)
    人月神话阅读笔记06
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3275542.html
Copyright © 2011-2022 走看看