結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー tentententen
提出日時 2020-08-20 10:30:33
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,574 bytes
コンパイル時間 3,712 ms
コンパイル使用メモリ 79,436 KB
実行使用メモリ 115,216 KB
最終ジャッジ日時 2024-04-21 05:15:37
合計ジャッジ時間 66,555 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
53,996 KB
testcase_01 AC 152 ms
53,976 KB
testcase_02 AC 1,682 ms
87,452 KB
testcase_03 AC 1,843 ms
103,316 KB
testcase_04 AC 1,852 ms
103,360 KB
testcase_05 AC 1,643 ms
102,008 KB
testcase_06 AC 1,648 ms
101,464 KB
testcase_07 AC 910 ms
74,460 KB
testcase_08 AC 1,695 ms
113,828 KB
testcase_09 AC 590 ms
61,528 KB
testcase_10 AC 1,278 ms
84,860 KB
testcase_11 AC 1,027 ms
80,408 KB
testcase_12 AC 1,131 ms
73,616 KB
testcase_13 AC 1,766 ms
89,844 KB
testcase_14 AC 1,849 ms
96,332 KB
testcase_15 AC 1,986 ms
101,796 KB
testcase_16 AC 1,432 ms
81,984 KB
testcase_17 TLE -
testcase_18 AC 1,227 ms
75,736 KB
testcase_19 AC 1,913 ms
102,948 KB
testcase_20 AC 1,096 ms
75,712 KB
testcase_21 AC 1,289 ms
77,576 KB
testcase_22 TLE -
testcase_23 AC 284 ms
57,960 KB
testcase_24 AC 303 ms
58,368 KB
testcase_25 AC 1,096 ms
73,420 KB
testcase_26 AC 1,487 ms
85,820 KB
testcase_27 AC 1,642 ms
84,984 KB
testcase_28 AC 1,878 ms
94,196 KB
testcase_29 AC 715 ms
63,108 KB
testcase_30 TLE -
testcase_31 AC 1,631 ms
87,528 KB
testcase_32 AC 1,227 ms
82,640 KB
testcase_33 AC 1,765 ms
103,916 KB
testcase_34 AC 1,260 ms
76,608 KB
testcase_35 AC 1,992 ms
101,480 KB
testcase_36 AC 240 ms
57,472 KB
testcase_37 AC 258 ms
57,776 KB
testcase_38 AC 245 ms
57,348 KB
testcase_39 AC 266 ms
58,344 KB
testcase_40 AC 212 ms
57,436 KB
testcase_41 TLE -
testcase_42 AC 1,269 ms
80,480 KB
testcase_43 AC 1,764 ms
86,888 KB
testcase_44 AC 1,012 ms
73,572 KB
testcase_45 AC 1,621 ms
85,684 KB
testcase_46 AC 136 ms
53,796 KB
testcase_47 AC 136 ms
53,700 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