結果
問題 |
No.1065 電柱 / Pole (Easy)
|
ユーザー |
![]() |
提出日時 | 2020-08-20 10:39:09 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,216 ms / 2,000 ms |
コード長 | 2,751 bytes |
コンパイル時間 | 2,430 ms |
コンパイル使用メモリ | 80,264 KB |
実行使用メモリ | 95,476 KB |
最終ジャッジ日時 | 2024-10-13 03:57:26 |
合計ジャッジ時間 | 34,814 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 46 |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int n = Integer.parseInt(first[0]); int m = Integer.parseInt(first[1]); String[] second = br.readLine().split(" ", 2); int x = Integer.parseInt(second[0]) - 1; int y = Integer.parseInt(second[1]) - 1; ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); Node[] nodes = new Node[n]; for (int i = 0; i < n; i++) { String[] line = br.readLine().split(" ", 2); nodes[i] = new Node(i, Integer.parseInt(line[0]), Integer.parseInt(line[1])); graph.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { String[] line = br.readLine().split(" ", 2); int a = Integer.parseInt(line[0]) - 1; int b = Integer.parseInt(line[1]) - 1; graph.get(a).add(b); graph.get(b).add(a); } double[] costs = new double[n]; Arrays.fill(costs, Double.MAX_VALUE); PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(x, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int next : graph.get(p.idx)) { queue.add(new Path(next, p.value + nodes[next].getDistance(nodes[p.idx]))); } } System.out.println(costs[y]); } 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 Node { int idx; int x; int y; public Node(int idx, int x, int y) { this.idx = idx; this.x = x; this.y = y; } public double getDistance(Node another) { return Math.sqrt(Math.pow(x - another.x, 2) + Math.pow(y - another.y, 2)); } public int hashCode() { return idx; } public boolean equals(Object o) { Node n = (Node)o; return idx == n.idx; } } }