結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー htensaihtensai
提出日時 2020-06-04 12:43:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,263 ms / 2,000 ms
コード長 2,482 bytes
コンパイル時間 3,604 ms
コンパイル使用メモリ 75,088 KB
実行使用メモリ 141,172 KB
最終ジャッジ日時 2023-08-19 02:41:27
合計ジャッジ時間 36,985 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,488 KB
testcase_01 AC 44 ms
49,408 KB
testcase_02 AC 718 ms
93,884 KB
testcase_03 AC 1,014 ms
125,444 KB
testcase_04 AC 1,105 ms
133,332 KB
testcase_05 AC 939 ms
133,576 KB
testcase_06 AC 905 ms
132,520 KB
testcase_07 AC 412 ms
76,076 KB
testcase_08 AC 849 ms
141,172 KB
testcase_09 AC 222 ms
62,288 KB
testcase_10 AC 560 ms
95,820 KB
testcase_11 AC 428 ms
78,312 KB
testcase_12 AC 387 ms
60,568 KB
testcase_13 AC 1,031 ms
90,852 KB
testcase_14 AC 1,008 ms
91,084 KB
testcase_15 AC 1,077 ms
98,904 KB
testcase_16 AC 745 ms
77,440 KB
testcase_17 AC 1,263 ms
113,952 KB
testcase_18 AC 604 ms
77,316 KB
testcase_19 AC 1,127 ms
102,312 KB
testcase_20 AC 528 ms
61,676 KB
testcase_21 AC 775 ms
69,984 KB
testcase_22 AC 1,260 ms
97,544 KB
testcase_23 AC 126 ms
38,124 KB
testcase_24 AC 138 ms
38,724 KB
testcase_25 AC 399 ms
60,112 KB
testcase_26 AC 754 ms
77,852 KB
testcase_27 AC 748 ms
80,844 KB
testcase_28 AC 1,078 ms
96,356 KB
testcase_29 AC 320 ms
48,896 KB
testcase_30 AC 1,024 ms
98,080 KB
testcase_31 AC 928 ms
89,156 KB
testcase_32 AC 600 ms
69,032 KB
testcase_33 AC 1,132 ms
98,036 KB
testcase_34 AC 652 ms
68,004 KB
testcase_35 AC 945 ms
99,748 KB
testcase_36 AC 85 ms
36,664 KB
testcase_37 AC 116 ms
38,664 KB
testcase_38 AC 92 ms
37,048 KB
testcase_39 AC 110 ms
37,744 KB
testcase_40 AC 68 ms
34,888 KB
testcase_41 AC 1,208 ms
130,752 KB
testcase_42 AC 569 ms
81,224 KB
testcase_43 AC 791 ms
98,244 KB
testcase_44 AC 382 ms
67,380 KB
testcase_45 AC 896 ms
98,684 KB
testcase_46 AC 42 ms
49,932 KB
testcase_47 AC 43 ms
49,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] first = br.readLine().split(" ", 2);
		int n = Integer.parseInt(first[0]);
		int m = Integer.parseInt(first[1]);
		String[] second = br.readLine().split(" ", 2);
		int x = Integer.parseInt(second[0]) - 1;
		int y = Integer.parseInt(second[1]) - 1;
	    Point[] points = new Point[n];
	    ArrayList<HashMap<Integer, Double>> graph = new ArrayList<>();
	    for (int i = 0; i < n; i++) {
	        String[] line = br.readLine().split(" ", 2);
	        points[i] = new Point(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
	        graph.add(new HashMap<>());
	    }
	    for (int i = 0; i < m; i++) {
	        String[] line = br.readLine().split(" ", 2);
	        int a = Integer.parseInt(line[0]) - 1;
	        int b = Integer.parseInt(line[1]) - 1;
	        double distance = points[a].getDistance(points[b]);
	        graph.get(a).put(b, distance);
	        graph.get(b).put(a, distance);
	    }
	    PriorityQueue<Path> queue = new PriorityQueue<>();
	    queue.add(new Path(x, 0));
	    boolean[] visited = new boolean[n];
	    double ans = 0;
	    while (queue.size() > 0) {
	        Path p = queue.poll();
	        if (visited[p.idx]) {
	            continue;
	        }
	        visited[p.idx] = true;
	        if (p.idx == y) {
	            ans = p.value;
	            break;
	        }
	        for (Map.Entry<Integer, Double> entry : graph.get(p.idx).entrySet()) {
	            if (!visited[entry.getKey()]) {
	                queue.add(new Path(entry.getKey(), p.value + entry.getValue()));
	            }
	        }
	    }
		System.out.println(ans);
	}
	
	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