結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー hiromi_ayasehiromi_ayase
提出日時 2020-05-29 21:41:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,704 ms / 2,000 ms
コード長 2,296 bytes
コンパイル時間 2,660 ms
コンパイル使用メモリ 79,596 KB
実行使用メモリ 84,752 KB
最終ジャッジ日時 2024-04-23 20:47:30
合計ジャッジ時間 48,983 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
41,216 KB
testcase_01 AC 123 ms
41,292 KB
testcase_02 AC 1,262 ms
72,976 KB
testcase_03 AC 1,408 ms
83,336 KB
testcase_04 AC 1,488 ms
81,928 KB
testcase_05 AC 1,556 ms
83,128 KB
testcase_06 AC 1,476 ms
78,972 KB
testcase_07 AC 773 ms
61,668 KB
testcase_08 AC 1,310 ms
83,452 KB
testcase_09 AC 535 ms
50,192 KB
testcase_10 AC 981 ms
68,896 KB
testcase_11 AC 856 ms
65,976 KB
testcase_12 AC 689 ms
56,052 KB
testcase_13 AC 1,272 ms
74,348 KB
testcase_14 AC 1,306 ms
78,176 KB
testcase_15 AC 1,444 ms
84,752 KB
testcase_16 AC 1,039 ms
68,748 KB
testcase_17 AC 1,502 ms
84,028 KB
testcase_18 AC 870 ms
65,084 KB
testcase_19 AC 1,487 ms
79,724 KB
testcase_20 AC 842 ms
63,368 KB
testcase_21 AC 985 ms
67,600 KB
testcase_22 AC 1,385 ms
77,572 KB
testcase_23 AC 227 ms
46,468 KB
testcase_24 AC 241 ms
47,136 KB
testcase_25 AC 636 ms
55,960 KB
testcase_26 AC 1,109 ms
70,680 KB
testcase_27 AC 1,122 ms
69,912 KB
testcase_28 AC 1,500 ms
78,952 KB
testcase_29 AC 529 ms
50,756 KB
testcase_30 AC 1,519 ms
82,392 KB
testcase_31 AC 1,257 ms
74,172 KB
testcase_32 AC 1,029 ms
69,052 KB
testcase_33 AC 1,407 ms
81,448 KB
testcase_34 AC 951 ms
67,652 KB
testcase_35 AC 1,490 ms
79,264 KB
testcase_36 AC 206 ms
46,860 KB
testcase_37 AC 234 ms
46,252 KB
testcase_38 AC 210 ms
46,884 KB
testcase_39 AC 220 ms
46,328 KB
testcase_40 AC 175 ms
43,444 KB
testcase_41 AC 1,704 ms
83,300 KB
testcase_42 AC 958 ms
65,732 KB
testcase_43 AC 1,192 ms
72,592 KB
testcase_44 AC 771 ms
60,944 KB
testcase_45 AC 1,219 ms
72,076 KB
testcase_46 AC 116 ms
41,120 KB
testcase_47 AC 115 ms
41,172 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[] { sc.nextInt(), sc.nextInt() };
        }

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