結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー htensai
提出日時 2020-06-04 11:58:07
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,834 ms / 2,000 ms
コード長 1,871 bytes
コンパイル時間 4,218 ms
コンパイル使用メモリ 79,408 KB
実行使用メモリ 102,468 KB
最終ジャッジ日時 2024-11-28 11:19:33
合計ジャッジ時間 55,065 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

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