| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
100题纪念一下,,加油,贴上java代码完全背包+刚好充满+最小值
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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator