結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー htensaihtensai
提出日時 2020-06-04 12:38:25
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,063 bytes
コンパイル時間 2,380 ms
コンパイル使用メモリ 76,392 KB
実行使用メモリ 282,640 KB
最終ジャッジ日時 2023-08-19 02:31:41
合計ジャッジ時間 10,502 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
60,428 KB
testcase_01 AC 112 ms
55,732 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int x = sc.nextInt() - 1;
		int y = sc.nextInt() - 1;
	    Point[] points = new Point[n];
	    ArrayList<HashMap<Integer, Double>> graph = new ArrayList<>();
	    for (int i = 0; i < n; i++) {
	        points[i] = new Point(sc.nextInt(), sc.nextInt());
	        graph.add(new HashMap<>());
	    }
	    for (int i = 0; i < m; i++) {
	        int a = sc.nextInt() - 1;
	        int b = sc.nextInt() - 1;
	        double distance = points[a].getDistance(points[b]);
	        graph.get(a).put(b, distance);
	        graph.get(b).put(a, distance);
	    }
	    PriorityQueue<Path> queue = new PriorityQueue<>();
	    queue.add(new Path(x, 0));
	    boolean[] visited = new boolean[n];
	    double ans = 0;
	    while (queue.size() > 0) {
	        Path p = queue.poll();
	        if (visited[p.idx]) {
	            continue;
	        }
	        if (p.idx == y) {
	            ans = p.value;
	            break;
	        }
	        for (Map.Entry<Integer, Double> entry : graph.get(p.idx).entrySet()) {
	            if (!visited[entry.getKey()]) {
	                queue.add(new Path(entry.getKey(), p.value + entry.getValue()));
	            }
	        }
	    }
		System.out.println(ans);
	}
	
	static class Path implements Comparable<Path> {
	    int idx;
	    double value;
	    
	    public Path(int idx, double value) {
	        this.idx = idx;
	        this.value = value;
	    }
	    
	    public int compareTo(Path another) {
	        if (value == another.value) {
	            return 0;
	        } else if (value < another.value) {
	            return -1;
	        } else {
	            return 1;
	        }
	    }
	}
	
	static class Point {
	    int x;
	    int y;
	    
	    public Point(int x, int y) {
	        this.x = x;
	        this.y = y;
	    }
	    
	    public double getDistance(Point p) {
	        return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
	    }
	}
}
0