結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-05-29 21:42:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,659 ms / 2,000 ms
コード長 2,356 bytes
コンパイル時間 2,411 ms
コンパイル使用メモリ 80,064 KB
実行使用メモリ 81,832 KB
最終ジャッジ日時 2024-04-23 20:59:29
合計ジャッジ時間 51,303 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
41,644 KB
testcase_01 AC 142 ms
41,572 KB
testcase_02 AC 1,273 ms
68,084 KB
testcase_03 AC 1,429 ms
78,444 KB
testcase_04 AC 1,463 ms
79,772 KB
testcase_05 AC 1,409 ms
81,356 KB
testcase_06 AC 1,481 ms
79,484 KB
testcase_07 AC 751 ms
57,824 KB
testcase_08 AC 1,318 ms
79,848 KB
testcase_09 AC 493 ms
50,316 KB
testcase_10 AC 964 ms
66,720 KB
testcase_11 AC 794 ms
60,312 KB
testcase_12 AC 705 ms
56,292 KB
testcase_13 AC 1,369 ms
73,404 KB
testcase_14 AC 1,481 ms
75,216 KB
testcase_15 AC 1,467 ms
77,336 KB
testcase_16 AC 1,096 ms
66,432 KB
testcase_17 AC 1,552 ms
79,924 KB
testcase_18 AC 946 ms
62,244 KB
testcase_19 AC 1,594 ms
77,692 KB
testcase_20 AC 887 ms
60,864 KB
testcase_21 AC 959 ms
65,560 KB
testcase_22 AC 1,472 ms
76,064 KB
testcase_23 AC 226 ms
46,016 KB
testcase_24 AC 233 ms
45,908 KB
testcase_25 AC 683 ms
55,084 KB
testcase_26 AC 1,205 ms
68,288 KB
testcase_27 AC 1,206 ms
68,020 KB
testcase_28 AC 1,567 ms
77,460 KB
testcase_29 AC 574 ms
49,260 KB
testcase_30 AC 1,596 ms
77,076 KB
testcase_31 AC 1,338 ms
72,136 KB
testcase_32 AC 1,030 ms
65,988 KB
testcase_33 AC 1,550 ms
77,504 KB
testcase_34 AC 943 ms
62,756 KB
testcase_35 AC 1,640 ms
80,884 KB
testcase_36 AC 216 ms
43,972 KB
testcase_37 AC 236 ms
46,284 KB
testcase_38 AC 225 ms
44,280 KB
testcase_39 AC 239 ms
46,824 KB
testcase_40 AC 196 ms
42,860 KB
testcase_41 AC 1,659 ms
81,832 KB
testcase_42 AC 994 ms
62,340 KB
testcase_43 AC 1,259 ms
68,600 KB
testcase_44 AC 750 ms
58,820 KB
testcase_45 AC 1,224 ms
70,684 KB
testcase_46 AC 135 ms
41,572 KB
testcase_47 AC 137 ms
41,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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;

        int[][] p = new int[n][];
        for (int i = 0; i < n; i++) {
            p[i] = new int[] { Integer.parseInt(sc.next()), Integer.parseInt(sc.next()) };
        }

        int[] from = new int[m];
        int[] to = new int[m];
        int[] w = new int[m];
        for (int i = 0; i < m; i ++) {
            from[i] = Integer.parseInt(sc.next()) - 1;
            to[i] = Integer.parseInt(sc.next()) - 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