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

Re:Java 水过,是参考网上的代码的

Posted by misakaface at 2015-04-05 17:35:33 on Problem 1062
In Reply To:Java 水过,是参考网上的代码的 Posted by:HeartLC at 2013-04-11 12:14:09
	import java.io.BufferedReader;
	import java.io.IOException;
	import java.io.InputStreamReader;
	import java.util.HashMap;
	import java.util.Iterator;
	import java.util.Set;
	public class Main{
		private static int Mleft;
		private static int M;
		private static int maxM;
		private static int minM;
		private static int N;
		private static HashMap<Integer,Integer[]> goods=new HashMap<Integer,Integer[]>();//<price,level>
		private static HashMap<Integer,HashMap<Integer,Integer>> replaces=new HashMap<Integer,HashMap<Integer,Integer>>();//<number,<index,discount>>
		public static void main(String[] args) throws IOException{
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String[] temps0=reader.readLine().split(" ");
			M=Integer.parseInt(temps0[0]);
			N=Integer.parseInt(temps0[1]);
			for(int i=0;i<N;i++){
				String[] temps=reader.readLine().split(" ");
				if(temps.length==3){
				goods.put(i, new Integer[]{Integer.parseInt(temps[0]),Integer.parseInt(temps[1]),Integer.parseInt(temps[2])});
				HashMap<Integer,Integer> tempReplace=new HashMap<Integer,Integer>();
					for(int j=0;j<Integer.parseInt(temps[2]);j++){
						String[] temps2=reader.readLine().split(" ");
						if(temps2.length==2){
							tempReplace.put(Integer.parseInt(temps2[0]), Integer.parseInt(temps2[1]));//number,discount
						}
					}
					replaces.put(i, tempReplace);
				}
			}
			System.out.println(DS(0));
		}
		//深度搜索
		private static int DS(int index) {
			int level=goods.get(index)[1];
			int min=goods.get(index)[0];
			int tryMin=min;
			try{
				Set<Integer> key = replaces.get(index).keySet();
				Iterator<Integer> it=key.iterator();
				while(it.hasNext()){//遍历所有替代品,如果更改第一个替代品,则重置权限范围
					if(index==0){
						 Mleft=M;
						 maxM=level;
						 minM=level;
						}
					int number=it.next()-1;
					int levl=goods.get(number)[1];
					int discount=replaces.get(index).get(number+1);
							if(maxM<levl||minM>levl){
								Mleft-=Math.abs(levl-level);
								if(Mleft>=0&&levl>level)
									maxM=levl;
								else if(Mleft>=0)
									minM=levl;
							}
							if(Mleft>=0){
								if(number>index)//限制环
									tryMin=discount+DS(number);
							}
							else
								Mleft+=Math.abs(levl-level);
					min=tryMin<min?tryMin:min;
				}
			}catch(Exception e){
				return index;
			}
			return min;
		}
	}

同JAVA水过,握手

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