結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー htensaihtensai
提出日時 2020-06-04 12:39:05
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,095 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 76,756 KB
実行使用メモリ 155,840 KB
最終ジャッジ日時 2023-08-19 02:34:07
合計ジャッジ時間 56,372 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,604 KB
testcase_01 AC 128 ms
56,016 KB
testcase_02 AC 1,332 ms
100,796 KB
testcase_03 AC 1,643 ms
132,164 KB
testcase_04 AC 1,781 ms
138,940 KB
testcase_05 AC 1,583 ms
134,068 KB
testcase_06 AC 1,676 ms
133,492 KB
testcase_07 AC 747 ms
82,060 KB
testcase_08 AC 1,610 ms
155,232 KB
testcase_09 AC 489 ms
63,088 KB
testcase_10 AC 1,020 ms
99,520 KB
testcase_11 AC 857 ms
89,688 KB
testcase_12 AC 748 ms
82,260 KB
testcase_13 AC 1,472 ms
116,024 KB
testcase_14 AC 1,507 ms
120,144 KB
testcase_15 AC 1,653 ms
124,608 KB
testcase_16 AC 1,231 ms
94,996 KB
testcase_17 AC 1,981 ms
130,368 KB
testcase_18 AC 1,055 ms
89,792 KB
testcase_19 AC 1,754 ms
128,104 KB
testcase_20 AC 891 ms
81,232 KB
testcase_21 AC 1,324 ms
94,828 KB
testcase_22 AC 1,717 ms
122,416 KB
testcase_23 AC 229 ms
59,652 KB
testcase_24 AC 232 ms
59,932 KB
testcase_25 AC 714 ms
81,092 KB
testcase_26 AC 1,184 ms
99,448 KB
testcase_27 AC 1,243 ms
98,004 KB
testcase_28 AC 1,715 ms
123,356 KB
testcase_29 AC 604 ms
70,608 KB
testcase_30 AC 1,723 ms
127,424 KB
testcase_31 AC 1,452 ms
106,868 KB
testcase_32 AC 1,064 ms
93,812 KB
testcase_33 AC 1,566 ms
124,452 KB
testcase_34 AC 1,145 ms
89,844 KB
testcase_35 AC 1,552 ms
128,564 KB
testcase_36 AC 211 ms
59,104 KB
testcase_37 AC 236 ms
59,480 KB
testcase_38 AC 224 ms
59,188 KB
testcase_39 AC 225 ms
59,156 KB
testcase_40 AC 187 ms
59,304 KB
testcase_41 TLE -
testcase_42 AC 985 ms
91,156 KB
testcase_43 AC 1,491 ms
114,000 KB
testcase_44 AC 771 ms
79,656 KB
testcase_45 AC 1,542 ms
104,020 KB
testcase_46 AC 122 ms
55,552 KB
testcase_47 AC 122 ms
55,716 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<HashMap<Integer, Double>> graph = new ArrayList<>();
	    for (int i = 0; i < n; i++) {
	        points[i] = new Point(sc.nextInt(), sc.nextInt());
	        graph.add(new HashMap<>());
	    }
	    for (int i = 0; i < m; i++) {
	        int a = sc.nextInt() - 1;
	        int b = sc.nextInt() - 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