結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー htensai
提出日時 2020-06-04 12:43:53
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,179 ms / 2,000 ms
コード長 2,482 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 79,404 KB
実行使用メモリ 141,428 KB
最終ジャッジ日時 2024-11-28 15:38:08
合計ジャッジ時間 32,575 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

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