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

BFS 猥琐搞定,5772K,1969MS。

Posted by Kuroro at 2012-12-06 22:23:22 on Problem 1837
网上把这道题目归到DP,自己DP不好,想不出来怎么递归,背包做了快一周,老觉得这道题目靠不到背包那种情况。到网上看了解题报告。拿着别人的代码步调理解意思,发现做的就是BFS的事情。。。。。。个人理解上,也觉得如果把这道题目归到BFS上面,理解起来会容易点。附上自己的代码,写的相当菜,大家尽管喷。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
//import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

//BFS + hash

//2012.12.6 16:00-22.16

public class Main{
	public static int nArm, nWeight;
	public static int MAX_NUM= 21;
	public static int[] armLen, weights;
	public static int MAX_STATUS = 0;
	public static void main(String args[]){
		//输入重定向
//		try {
//			System.setIn(new FileInputStream("G:/Java_workspace/acm_pku/bin/inputs/P1837input.txt"));
////			System.setOut(new PrintStream("G:/Java_workspace/acm_pku/bin/inputs/P1061output.txt"));	
//		} catch (FileNotFoundException e) {
//			System.out.println("redirect error");
//			e.printStackTrace();
//		}	
		Scanner in= new Scanner(System.in);	
		
		int i,j; 		
		while(in.hasNextInt()){			
			nArm = in.nextInt();			
			nWeight = in.nextInt();
//			System.out.println("nArm="+nArm+" nWeight="+nWeight);
		
			armLen = new int[MAX_NUM+1];
			weights = new int[MAX_NUM+1];			
			
			for( i = 1; i <= nArm; i++){
				armLen[i]= in.nextInt();  
			}
//			printout("armLen",armLen);

			for( i = 1; i <= nWeight; i++){
				weights[i]= in.nextInt();
			}
		
			HashMap<Integer, Integer> map1 = new HashMap<Integer, Integer>();
			HashMap<Integer, Integer> map2 = new HashMap<Integer, Integer>();  
			map1.put(MAX_STATUS, 1);
//			printmap(map1);
			
			for(i=1;i<=nWeight;i++){
				Iterator iter = map1.entrySet().iterator(); 
				while (iter.hasNext()) { 
					Map.Entry entry = (Map.Entry) iter.next(); 
					Integer key = (Integer)entry.getKey(); 
					Integer val = (Integer)entry.getValue(); 
						
					for(j=1;j<=nArm;j++)
					{
						int newpos = key+weights[i]*armLen[j];	
						
						if(map2.containsKey(newpos)){
							int oldval = map2.get(newpos);
							map2.put(newpos, oldval+val);
						}
						else map2.put(newpos, val);
					}
				} 
				map1 = map2;
//				printmap(map1);
				map2 = new HashMap<Integer, Integer>(); 
			}
			
//			printmap(map1);
			if(map1.containsKey(MAX_STATUS))
				System.out.println(map1.get(MAX_STATUS));
			else System.out.println(0);
				
			
		}
	}

//	private static void printmap(HashMap<Integer, Integer> map) {
//		Iterator iter = map.entrySet().iterator(); 
//		while (iter.hasNext()) {  
//			Map.Entry entry = (Map.Entry) iter.next(); 
//			Integer key = (Integer)entry.getKey(); 
//			Integer val = (Integer)entry.getValue(); 
//			System.out.print(key+" "+val+";");
//		 }
//		 System.out.println();
//		
//	}

//	private static void printout(String s, int[] res) {
//		if(res==null){
//			System.out.println("null");
//			return;
//		}
//		System.out.print(s+" len="+ res.length+" ");
//		for(int i=0;i<res.length; i++)
//			System.out.print(res[i]+" ");
//		System.out.println();
//	}
}

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