首页 > 专题系列 > Java解POJ > POJ 2502 Subway [解题报告] Java
2013
11-11

POJ 2502 Subway [解题报告] Java

Subway

问题描述 :

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don’t want to be late for class, you want to know how long it will take you to get to school.

You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.

输入:

Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.

输出:

Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.

样例输入:

0 0 10000 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1

样例输出:

21

解题代码:

//* @author:
import java.util.Scanner;
import java.io.*;

public class Main{
 double max = Double.MAX_VALUE;
 int fin[] = new int[205];
 double d[] = new double[205];
 double dis[][] = new double[205][205];
 pos p[] = new pos[205];

 void initial() {
  for (int i = 0; i < 205; i++)
	p[i] = new pos();
  Scanner s;
  int n = 2;
  try {
    s = new Scanner(System.in);
    p[0].x = s.nextInt();
    p[0].y = s.nextInt();
    p[1].x = s.nextInt();
    p[1].y = s.nextInt();

    int flag = 0;
    while (s.hasNext()) {
	p[n].x = s.nextInt();
	p[n].y = s.nextInt();
	if (p[n].x == -1 && p[n].y == -1) {
         flag = 0;
	  continue;
	}
	if (flag == 1) {
	  dis[n][n - 1] = dis[n - 1][n] = Math
	   .sqrt((p[n].x - p[n - 1].x) * (p[n].x - p[n - 1].x)
	   + (p[n].y - p[n - 1].y)
	   * (p[n].y - p[n - 1].y)) / 40000;
	}
	n++;
	flag = 1;
     }
   } catch (Exception e1) {
   }

  for (int i = 0; i < n; i++)
	for (int j = 0; j < n; j++)
         if (i != j && dis[i][j] == 0)
	    dis[i][j] = dis[j][i] = (Math.sqrt((p[j].x - p[i].x)
		* (p[j].x - p[i].x) + (p[j].y - p[i].y)
		* (p[j].y - p[i].y))) / 10000.0;

  fin[0] = 1;
  for (int i = 0; i < n; i++)
	d[i] = dis[0][i];
  int v = 0;
  for (int i = 1; i < n; i++) {
	double min = max;
       for (int j = 0; j < n; j++)
  		if (fin[j] != 1)
	        if (d[j] < min) {
		   v = j;
		   min = d[j];
		 }
		fin[v] = 1;
		for (int w = 0; w < n; w++)
		  if (fin[w] != 1 && (min + dis[v][w] < d[w]))
			d[w] = min + dis[v][w];

    }
    double ans = 60.0 * d[1];
    System.out.println(Math.round(ans));
 }

  public static void main(String[] args) {
	Main di = new Main ();
	di.initial();
  }
}

class pos {
	int x = 0;
	int y = 0;
}