Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

100题纪念一下,,加油,贴上java代码

Posted by lpp001 at 2012-12-25 19:41:07 on Problem 1384
完全背包+刚好充满+最小值
package dp.onezoropackage;
import java.io.*;
public class Acm1384 {
	public static int totalWeight;
	public static int []value;
	public static int []weight;
	public static int []record;
	public static int inf=99999999;
	
	/**
	 * @param args
	 */
	public static void main(String[] args)throws IOException {
		// TODO Auto-generated method stub
		StreamTokenizer input=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		input.nextToken();
		int text=(int)input.nval;
		while(text--!=0){
			input.nextToken();
			int a=(int)input.nval;
			input.nextToken();
			int b=(int)input.nval;
			input.nextToken();
			totalWeight=b-a;
			int k=(int)input.nval;
			value=new int[k+1];
			weight=new int[k+1];
			for(int i=1;i<k+1;i++){
				input.nextToken();
				value[i]=(int)input.nval;
				input.nextToken();
				weight[i]=(int)input.nval;
			}
			record=new int[totalWeight+1];
			for(int i=1;i<totalWeight+1;i++){
				record[i]=inf;
			}
			record[0]=0;
			for(int i=1;i<k+1;i++){
				for(int j=weight[i];j<totalWeight+1;j++){
					if(record[j-weight[i]]!=inf){
						record[j]=record[j]<(record[j-weight[i]]+value[i])?record[j]:(record[j-weight[i]]+value[i]);
					}
				}
			}
			if(record[totalWeight]==inf)System.out.println("This is impossible.");
			else System.out.println("The minimum amount of money in the piggy-bank is "+record[totalWeight]+".");
		}
	}

}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator