結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー tentententen
提出日時 2020-08-20 10:35:20
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,347 bytes
コンパイル時間 2,916 ms
コンパイル使用メモリ 80,156 KB
実行使用メモリ 113,968 KB
最終ジャッジ日時 2024-04-21 05:22:34
合計ジャッジ時間 59,586 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
53,888 KB
testcase_01 AC 150 ms
54,528 KB
testcase_02 AC 1,441 ms
86,224 KB
testcase_03 AC 1,830 ms
106,684 KB
testcase_04 AC 1,986 ms
103,052 KB
testcase_05 AC 1,714 ms
103,188 KB
testcase_06 AC 1,763 ms
102,588 KB
testcase_07 AC 782 ms
72,788 KB
testcase_08 AC 1,678 ms
113,808 KB
testcase_09 AC 592 ms
61,480 KB
testcase_10 AC 1,138 ms
85,256 KB
testcase_11 AC 975 ms
79,532 KB
testcase_12 AC 1,082 ms
75,288 KB
testcase_13 AC 1,557 ms
86,516 KB
testcase_14 AC 1,638 ms
98,292 KB
testcase_15 AC 1,770 ms
100,080 KB
testcase_16 AC 1,398 ms
79,092 KB
testcase_17 AC 1,924 ms
103,124 KB
testcase_18 AC 1,164 ms
76,616 KB
testcase_19 AC 1,838 ms
100,884 KB
testcase_20 AC 1,043 ms
75,584 KB
testcase_21 AC 1,315 ms
77,564 KB
testcase_22 AC 1,691 ms
98,852 KB
testcase_23 AC 270 ms
58,220 KB
testcase_24 AC 286 ms
58,168 KB
testcase_25 AC 1,100 ms
75,408 KB
testcase_26 AC 1,373 ms
83,568 KB
testcase_27 AC 1,390 ms
82,776 KB
testcase_28 AC 1,753 ms
97,704 KB
testcase_29 AC 687 ms
63,572 KB
testcase_30 AC 1,855 ms
97,792 KB
testcase_31 AC 1,463 ms
85,896 KB
testcase_32 AC 1,111 ms
82,080 KB
testcase_33 AC 1,627 ms
100,744 KB
testcase_34 AC 1,085 ms
78,212 KB
testcase_35 AC 1,737 ms
100,392 KB
testcase_36 AC 221 ms
57,532 KB
testcase_37 AC 245 ms
57,600 KB
testcase_38 AC 224 ms
57,536 KB
testcase_39 AC 254 ms
58,092 KB
testcase_40 AC 220 ms
56,404 KB
testcase_41 TLE -
testcase_42 AC 1,144 ms
79,576 KB
testcase_43 AC 1,454 ms
86,784 KB
testcase_44 AC 915 ms
73,500 KB
testcase_45 AC 1,389 ms
86,748 KB
testcase_46 AC 138 ms
53,856 KB
testcase_47 AC 139 ms
54,092 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;
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        Node[] nodes = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node(i, sc.nextInt(), sc.nextInt());
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 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;
        }
    }
} 
0