結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー htensaihtensai
提出日時 2020-06-04 11:58:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,907 ms / 2,000 ms
コード長 1,871 bytes
コンパイル時間 5,012 ms
コンパイル使用メモリ 77,288 KB
実行使用メモリ 109,372 KB
最終ジャッジ日時 2023-08-18 23:43:54
合計ジャッジ時間 60,529 ms
ジャッジサーバーID
(参考情報)
judge10 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
39,040 KB
testcase_01 AC 126 ms
39,200 KB
testcase_02 AC 1,376 ms
68,736 KB
testcase_03 AC 1,594 ms
85,464 KB
testcase_04 AC 1,693 ms
87,720 KB
testcase_05 AC 1,483 ms
96,148 KB
testcase_06 AC 1,543 ms
88,960 KB
testcase_07 AC 780 ms
58,048 KB
testcase_08 AC 1,568 ms
95,540 KB
testcase_09 AC 488 ms
48,020 KB
testcase_10 AC 1,027 ms
66,012 KB
testcase_11 AC 859 ms
61,732 KB
testcase_12 AC 950 ms
55,608 KB
testcase_13 AC 1,404 ms
72,136 KB
testcase_14 AC 1,510 ms
75,480 KB
testcase_15 AC 1,683 ms
81,460 KB
testcase_16 AC 1,188 ms
64,672 KB
testcase_17 AC 1,642 ms
81,640 KB
testcase_18 AC 1,019 ms
56,476 KB
testcase_19 AC 1,540 ms
86,864 KB
testcase_20 AC 879 ms
63,884 KB
testcase_21 AC 1,233 ms
60,036 KB
testcase_22 AC 1,491 ms
80,320 KB
testcase_23 AC 252 ms
53,340 KB
testcase_24 AC 269 ms
45,460 KB
testcase_25 AC 933 ms
55,540 KB
testcase_26 AC 1,275 ms
66,104 KB
testcase_27 AC 1,290 ms
65,120 KB
testcase_28 AC 1,636 ms
78,460 KB
testcase_29 AC 648 ms
60,264 KB
testcase_30 AC 1,591 ms
83,820 KB
testcase_31 AC 1,307 ms
76,744 KB
testcase_32 AC 1,138 ms
64,084 KB
testcase_33 AC 1,455 ms
79,232 KB
testcase_34 AC 1,075 ms
65,552 KB
testcase_35 AC 1,691 ms
97,452 KB
testcase_36 AC 213 ms
52,660 KB
testcase_37 AC 245 ms
44,088 KB
testcase_38 AC 223 ms
44,036 KB
testcase_39 AC 267 ms
44,296 KB
testcase_40 AC 204 ms
41,760 KB
testcase_41 AC 1,907 ms
109,372 KB
testcase_42 AC 1,034 ms
69,348 KB
testcase_43 AC 1,362 ms
69,556 KB
testcase_44 AC 819 ms
55,484 KB
testcase_45 AC 1,361 ms
68,556 KB
testcase_46 AC 122 ms
56,236 KB
testcase_47 AC 121 ms
55,720 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;
	    Point[] points = new Point[n];
	    ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
	    for (int i = 0; i < n; i++) {
	        points[i] = new Point(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);
	    }
	    PriorityQueue<Path> queue = new PriorityQueue<>();
	    queue.add(new Path(x, 0));
	    double[] costs = new double[n];
	    Arrays.fill(costs, Double.MAX_VALUE);
	    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 + points[p.idx].getDistance(points[next])));
	        }
	    }
		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 Point {
	    int x;
	    int y;
	    
	    public Point(int x, int y) {
	        this.x = x;
	        this.y = y;
	    }
	    
	    public double getDistance(Point p) {
	        return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
	    }
	}
}
0