Frogger http://acm.pku.edu.cn/JudgeOnline/problem?id=2253

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 8109
Accepted: 2772

Description

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.

Input

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

Sample Input

2
0 0
3 4

3
17 4
19 4
18 5

0

Sample Output

Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

----------------------------------------

Solution

Prim’s algorithm to find the partial MST which ends at node # 2.

import java.text.DecimalFormat;
import java.util.*;

public class POJ_2253 {
	static ArrayList<double[]> data = new ArrayList<double[]>();
	static ArrayList<double[]> old_data = new ArrayList<double[]>();
	static ArrayList<double[]> new_data = new ArrayList<double[]>();

	static class comparator implements Comparator<double[]> {
		public int compare(double[] arg0, double[] arg1) {
			if (arg0[2] > arg1[2])
				return 1;
			if (arg0[2] < arg1[2]) {
				return -1;
			} else
				return 0;
		}

	}

	public static double prim() {
		PriorityQueue<double[]> pq = new PriorityQueue<double[]>(10,
				new comparator());
		PriorityQueue<double[]> edges = new PriorityQueue<double[]>(10,
				new comparator());
		new_data.add(old_data.remove(0));
		new_data.trimToSize();
		while (old_data.size() != 0) {
			pq.clear();
			for (int i = 0; i < new_data.size(); i++) {
				for (int j = 0; j < old_data.size(); j++) {
					double dist[] = new double[3];
					dist[0] = new_data.get(i)[0];
					dist[1] = old_data.get(j)[0];
					double distance = Math.sqrt(Math.pow(new_data.get(i)[1]
							- old_data.get(j)[1], 2)
							+ Math.pow(new_data.get(i)[2] - old_data.get(j)[2], 2));
					dist[2] = distance;
					pq.add(dist);
				}
			}
			double temp[] = pq.remove();
			new_data.add(data.get((int)temp[1]));
			old_data.remove(data.get((int)temp[1]));
			new_data.trimToSize();
			old_data.trimToSize();
			edges.add(temp);
			if(temp[1] == 1){
				break;
			}
		}
		int i = edges.size();
		Iterator<double[]> it = edges.iterator();
		int j = 0;
		while(j++ != i - 1){
			it.next();
		}
		return it.next()[2];
	}

	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		int num = in.nextInt();
		double temp[];

		for (int i = 0; i < num; i++) {
			temp = new double[3];
			temp[0] = i;
			temp[1] = in.nextDouble();
			temp[2] = in.nextDouble();
			data.add(temp);
		}
		data.trimToSize();
		old_data.addAll(data);
		double out = prim();
		String pattern = "#.###";
		DecimalFormat decimal = new DecimalFormat(pattern);
		
		System.out.println(decimal.format(out));
	}
}
posted on 2010-01-17 04:15  流光星烁  阅读(793)  评论(0)    收藏  举报