結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー htensaihtensai
提出日時 2020-06-04 12:43:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,212 ms / 2,000 ms
コード長 2,482 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 82,844 KB
実行使用メモリ 129,936 KB
最終ジャッジ日時 2024-05-06 09:50:42
合計ジャッジ時間 32,774 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
37,044 KB
testcase_01 AC 45 ms
37,128 KB
testcase_02 AC 650 ms
84,332 KB
testcase_03 AC 912 ms
111,064 KB
testcase_04 AC 1,060 ms
123,108 KB
testcase_05 AC 883 ms
110,416 KB
testcase_06 AC 823 ms
122,356 KB
testcase_07 AC 400 ms
64,260 KB
testcase_08 AC 823 ms
125,028 KB
testcase_09 AC 211 ms
51,084 KB
testcase_10 AC 555 ms
82,564 KB
testcase_11 AC 407 ms
66,984 KB
testcase_12 AC 364 ms
62,648 KB
testcase_13 AC 916 ms
91,540 KB
testcase_14 AC 992 ms
92,732 KB
testcase_15 AC 983 ms
102,508 KB
testcase_16 AC 684 ms
79,012 KB
testcase_17 AC 1,163 ms
113,848 KB
testcase_18 AC 574 ms
66,076 KB
testcase_19 AC 1,079 ms
117,400 KB
testcase_20 AC 464 ms
62,320 KB
testcase_21 AC 721 ms
78,676 KB
testcase_22 AC 1,007 ms
98,344 KB
testcase_23 AC 119 ms
40,992 KB
testcase_24 AC 133 ms
41,432 KB
testcase_25 AC 344 ms
62,880 KB
testcase_26 AC 609 ms
79,576 KB
testcase_27 AC 673 ms
80,616 KB
testcase_28 AC 993 ms
98,800 KB
testcase_29 AC 293 ms
52,440 KB
testcase_30 AC 966 ms
102,764 KB
testcase_31 AC 871 ms
89,204 KB
testcase_32 AC 575 ms
70,548 KB
testcase_33 AC 1,101 ms
101,324 KB
testcase_34 AC 609 ms
69,052 KB
testcase_35 AC 1,013 ms
114,080 KB
testcase_36 AC 106 ms
39,516 KB
testcase_37 AC 118 ms
41,288 KB
testcase_38 AC 99 ms
39,652 KB
testcase_39 AC 124 ms
41,232 KB
testcase_40 AC 76 ms
38,348 KB
testcase_41 AC 1,212 ms
129,936 KB
testcase_42 AC 550 ms
68,072 KB
testcase_43 AC 775 ms
87,500 KB
testcase_44 AC 342 ms
57,168 KB
testcase_45 AC 845 ms
89,036 KB
testcase_46 AC 44 ms
37,064 KB
testcase_47 AC 45 ms
37,056 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