結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-05-29 21:50:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,051 ms / 2,000 ms
コード長 3,373 bytes
コンパイル時間 2,981 ms
コンパイル使用メモリ 79,356 KB
実行使用メモリ 89,616 KB
最終ジャッジ日時 2024-04-23 21:27:49
合計ジャッジ時間 31,636 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,800 KB
testcase_01 AC 57 ms
50,248 KB
testcase_02 AC 736 ms
73,424 KB
testcase_03 AC 922 ms
82,492 KB
testcase_04 AC 971 ms
82,484 KB
testcase_05 AC 781 ms
81,556 KB
testcase_06 AC 843 ms
83,392 KB
testcase_07 AC 341 ms
64,968 KB
testcase_08 AC 696 ms
84,812 KB
testcase_09 AC 216 ms
55,844 KB
testcase_10 AC 454 ms
69,292 KB
testcase_11 AC 373 ms
65,284 KB
testcase_12 AC 312 ms
61,308 KB
testcase_13 AC 786 ms
75,836 KB
testcase_14 AC 873 ms
79,276 KB
testcase_15 AC 932 ms
87,156 KB
testcase_16 AC 616 ms
68,508 KB
testcase_17 AC 955 ms
82,356 KB
testcase_18 AC 466 ms
65,384 KB
testcase_19 AC 1,051 ms
78,548 KB
testcase_20 AC 440 ms
63,180 KB
testcase_21 AC 513 ms
67,476 KB
testcase_22 AC 877 ms
79,364 KB
testcase_23 AC 128 ms
52,348 KB
testcase_24 AC 124 ms
52,356 KB
testcase_25 AC 298 ms
62,924 KB
testcase_26 AC 656 ms
71,200 KB
testcase_27 AC 666 ms
69,508 KB
testcase_28 AC 968 ms
80,900 KB
testcase_29 AC 270 ms
60,956 KB
testcase_30 AC 1,010 ms
82,628 KB
testcase_31 AC 795 ms
72,620 KB
testcase_32 AC 609 ms
67,488 KB
testcase_33 AC 974 ms
79,536 KB
testcase_34 AC 492 ms
67,820 KB
testcase_35 AC 1,045 ms
79,608 KB
testcase_36 AC 109 ms
52,116 KB
testcase_37 AC 136 ms
53,128 KB
testcase_38 AC 125 ms
52,368 KB
testcase_39 AC 140 ms
53,152 KB
testcase_40 AC 88 ms
51,456 KB
testcase_41 AC 1,039 ms
89,616 KB
testcase_42 AC 497 ms
65,248 KB
testcase_43 AC 718 ms
71,188 KB
testcase_44 AC 404 ms
63,232 KB
testcase_45 AC 705 ms
71,788 KB
testcase_46 AC 55 ms
50,328 KB
testcase_47 AC 55 ms
50,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {

    private static java.io.InputStream is = System.in;
    private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
    private static java.util.StringTokenizer tokenizer = null;
    private static java.io.BufferedReader reader;
    static {
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
    }

    public static String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new java.util.StringTokenizer(reader.readLine());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    private static int ni() {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) {
        int n = ni();
        int m = ni();
        int x = ni() - 1;
        int y = ni() - 1;

        int[][] p = new int[n][];
        for (int i = 0; i < n; i++) {
            p[i] = new int[] { ni(), ni() };
        }

        int[] from = new int[m];
        int[] to = new int[m];
        int[] w = new int[m];
        for (int i = 0; i < m; i++) {
            from[i] = ni() - 1;
            to[i] = ni() - 1;

            int dx = p[from[i]][0] - p[to[i]][0];
            int dy = p[from[i]][1] - p[to[i]][1];
            w[i] = dx * dx + dy * dy;
        }

        int[][][] g = packWU(n, from, to, w);
        double[] d = dijk(g, x);
        System.out.println(d[y]);
    }

    public static int[][][] packWU(int n, int[] from, int[] to, int[] w) {
        return packWU(n, from, to, w, from.length);
    }

    public static int[][][] packWU(int n, int[] from, int[] to, int[] w, int sup) {
        int[][][] g = new int[n][][];
        int[] p = new int[n];
        for (int i = 0; i < sup; i++)
            p[from[i]]++;
        for (int i = 0; i < sup; i++)
            p[to[i]]++;
        for (int i = 0; i < n; i++)
            g[i] = new int[p[i]][2];
        for (int i = 0; i < sup; i++) {
            --p[from[i]];
            g[from[i]][p[from[i]]][0] = to[i];
            g[from[i]][p[from[i]]][1] = w[i];
            --p[to[i]];
            g[to[i]][p[to[i]]][0] = from[i];
            g[to[i]][p[to[i]]][1] = w[i];
        }
        return g;
    }

    public static double[] dijk(int[][][] g, int from) {
        int n = g.length;
        final double[] td = new double[n];
        Arrays.fill(td, Integer.MAX_VALUE);
        TreeSet<Integer> q = new TreeSet<Integer>(new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                if (td[a] - td[b] != 0)
                    return Double.compare(td[a], td[b]);
                return a - b;
            }
        });
        q.add(from);
        td[from] = 0;

        while (q.size() > 0) {
            int cur = q.pollFirst();

            for (int i = 0; i < g[cur].length; i++) {
                int next = g[cur][i][0];
                double nd = td[cur] + Math.sqrt(g[cur][i][1]);
                if (nd < td[next]) {
                    q.remove(next);
                    td[next] = nd;
                    q.add(next);
                }
            }
        }

        return td;
    }
}
0