POJ 2502 subway 演算法的问题DescriptionYou 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 takethe subway.Because you don't want to be late

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/02 07:47:31
POJ 2502 subway 演算法的问题DescriptionYou 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 takethe subway.Because you don't want to be late

POJ 2502 subway 演算法的问题DescriptionYou 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 takethe subway.Because you don't want to be late
POJ 2502 subway 演算法的问题
Description
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 W (km/h).The subway travels at S (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
The first input line are W and S.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
The first line is the number of minutes it will take you to get to school,rounded to
the nearest minute,taking the fastest route.Then print the fastest route.
-------------------------------
想请问此题目的程式码要如何作答?
不是C++作答喔

POJ 2502 subway 演算法的问题DescriptionYou 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 takethe subway.Because you don't want to be late
最短路径算法,Dijkstra或者Floyd
Java你不会自己写吗.
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{

\x05public static double cal(int x, int y, int nx, int ny, int speed) {
\x05\x05int dx = x - nx;
\x05\x05int dy = y - ny;
\x05\x05return 1.0 * Math.sqrt(dx*dx + dy*dy) / speed;
\x05}
\x05public static void main (String[] args) throws java.lang.Exception
\x05{
\x05\x05// your code goes here
\x05\x05Scanner cin = new Scanner(System.in);
\x05\x05int[] x = new int[300];
\x05\x05int[] y = new int[300];
\x05\x05double[][] dist = new double[300][300];
\x05\x05for(int i=0;i<300;i++) {
\x05\x05\x05for(int j=0;j<300;j++) {
\x05\x05\x05\x05dist[i][j] = -1;
\x05\x05\x05}
\x05\x05}
\x05\x05int num = 2;
\x05\x05x[0] = cin.nextInt();
\x05\x05y[0] = cin.nextInt();
\x05\x05x[1] = cin.nextInt();
\x05\x05y[1] = cin.nextInt();
\x05\x05while(cin.hasNextInt()) {
\x05\x05\x05int fx, fy, nx, ny;
\x05\x05\x05fx = cin.nextInt();
\x05\x05\x05fy = cin.nextInt();
\x05\x05\x05x[num] = fx;
\x05\x05\x05y[num] = fy;
\x05\x05\x05num = num + 1;
\x05\x05\x05do {
\x05\x05\x05\x05nx = cin.nextInt();
\x05\x05\x05\x05ny = cin.nextInt();
\x05\x05\x05\x05if(nx == -1 && ny == -1) break;
\x05\x05\x05\x05x[num] = nx;
\x05\x05\x05\x05y[num] = ny;
\x05\x05\x05\x05num = num + 1;
\x05\x05\x05\x05dist[num-1][num-2] = dist[num-2][num-1] = cal(x[num-2],y[num-2],x[num-1],y[num-1],40000);
\x05\x05\x05\x05fx = nx;
\x05\x05\x05\x05fy = ny;
\x05\x05\x05}while(true);
\x05\x05}
\x05\x05for(int i=0;i<num;i++) {
\x05\x05\x05dist[i][i] = 0;
\x05\x05\x05for(int j=i+1;j<num;j++) {
\x05\x05\x05\x05if(dist[i][j] < 0) {
\x05\x05\x05\x05\x05dist[i][j] = dist[j][i] = cal(x[i],y[i],x[j],y[j],10000);
\x05\x05\x05\x05}
\x05\x05\x05}
\x05\x05}
\x05\x05for(int k=0;k<num;k++) {
\x05\x05\x05for(int i=0;i<num;i++) {
\x05\x05\x05\x05for(int j=0;j<num;j++) {
\x05\x05\x05\x05\x05if(dist[i][j] > dist[i][k] + dist[k][j]) {
\x05\x05\x05\x05\x05\x05dist[i][j] = dist[i][k] + dist[k][j];
\x05\x05\x05\x05\x05}
\x05\x05\x05\x05}
\x05\x05\x05}
\x05\x05}
\x05\x05double ans = dist[0][1] * 60;
\x05\x05int ansint = (int)Math.floor(ans + 0.5);
\x05\x05System.out.printf("%d%n",ansint);
\x05}
}