結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー tentententen
提出日時 2020-08-20 10:30:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,979 ms / 2,000 ms
コード長 2,574 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 79,752 KB
実行使用メモリ 106,484 KB
最終ジャッジ日時 2024-10-13 03:45:19
合計ジャッジ時間 56,511 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
40,948 KB
testcase_01 AC 131 ms
41,800 KB
testcase_02 AC 1,471 ms
75,824 KB
testcase_03 AC 1,502 ms
97,424 KB
testcase_04 AC 1,718 ms
92,864 KB
testcase_05 AC 1,392 ms
90,312 KB
testcase_06 AC 1,471 ms
92,996 KB
testcase_07 AC 730 ms
66,588 KB
testcase_08 AC 1,466 ms
103,940 KB
testcase_09 AC 517 ms
50,600 KB
testcase_10 AC 1,103 ms
74,484 KB
testcase_11 AC 912 ms
68,356 KB
testcase_12 AC 924 ms
62,472 KB
testcase_13 AC 1,549 ms
76,664 KB
testcase_14 AC 1,555 ms
84,776 KB
testcase_15 AC 1,719 ms
92,356 KB
testcase_16 AC 1,255 ms
69,284 KB
testcase_17 AC 1,713 ms
93,684 KB
testcase_18 AC 1,054 ms
64,848 KB
testcase_19 AC 1,622 ms
92,604 KB
testcase_20 AC 957 ms
64,680 KB
testcase_21 AC 1,194 ms
67,660 KB
testcase_22 AC 1,632 ms
86,920 KB
testcase_23 AC 243 ms
47,040 KB
testcase_24 AC 269 ms
47,524 KB
testcase_25 AC 1,018 ms
63,608 KB
testcase_26 AC 1,244 ms
75,292 KB
testcase_27 AC 1,275 ms
73,944 KB
testcase_28 AC 1,570 ms
85,040 KB
testcase_29 AC 636 ms
52,540 KB
testcase_30 AC 1,665 ms
94,076 KB
testcase_31 AC 1,451 ms
78,764 KB
testcase_32 AC 1,193 ms
73,372 KB
testcase_33 AC 1,530 ms
92,408 KB
testcase_34 AC 1,140 ms
66,240 KB
testcase_35 AC 1,716 ms
91,492 KB
testcase_36 AC 215 ms
45,972 KB
testcase_37 AC 233 ms
46,836 KB
testcase_38 AC 222 ms
45,860 KB
testcase_39 AC 254 ms
46,672 KB
testcase_40 AC 194 ms
43,948 KB
testcase_41 AC 1,979 ms
106,484 KB
testcase_42 AC 1,085 ms
70,008 KB
testcase_43 AC 1,365 ms
81,144 KB
testcase_44 AC 840 ms
64,268 KB
testcase_45 AC 1,381 ms
76,852 KB
testcase_46 AC 123 ms
41,504 KB
testcase_47 AC 111 ms
41,208 KB
権限があれば一括ダウンロードができます

ソースコード

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;
        Node[] nodes = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node(i, sc.nextInt(), sc.nextInt());
        }
        HashMap<Node, ArrayList<Node>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            if (!graph.containsKey(nodes[a])) {
                graph.put(nodes[a], new ArrayList<>());
            }
            graph.get(nodes[a]).add(nodes[b]);
            if (!graph.containsKey(nodes[b])) {
                graph.put(nodes[b], new ArrayList<>());
            }
            graph.get(nodes[b]).add(nodes[a]);
        }
        double[] costs = new double[n];
        Arrays.fill(costs, Double.MAX_VALUE);
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(nodes[x], 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.node.idx] <= p.value) {
                continue;
            }
            costs[p.node.idx] = p.value;
            for (Node nd : graph.get(p.node)) {
                queue.add(new Path(nd, p.value + p.node.getDistance(nd)));
            }
        }
        System.out.println(costs[y]);
    }
    

    static class Path implements Comparable<Path> {
        Node node;
        double value;

        public Path(Node node, double value) {
            this.node = node;
            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;
        }
    }
} 
0