結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー ks2m
提出日時 2020-05-29 21:56:15
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,070 ms / 2,000 ms
コード長 1,940 bytes
コンパイル時間 2,677 ms
コンパイル使用メモリ 78,700 KB
実行使用メモリ 96,636 KB
最終ジャッジ日時 2024-11-06 04:13:56
合計ジャッジ時間 31,579 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		sa = br.readLine().split(" ");
		int x = Integer.parseInt(sa[0]) - 1;
		int y = Integer.parseInt(sa[1]) - 1;
		int[] p = new int[n];
		int[] q = new int[n];
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			p[i] = Integer.parseInt(sa[0]);
			q[i] = Integer.parseInt(sa[1]);
		}

		List<List<Hen>> list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			int a = Integer.parseInt(sa[0]) - 1;
			int b = Integer.parseInt(sa[1]) - 1;
			double c = Math.hypot(p[a] - p[b], q[a] - q[b]);

			list.get(a).add(new Hen(b, c));
			list.get(b).add(new Hen(a, c));
		}
		br.close();

		double[] d = new double[list.size()];
		Arrays.fill(d, Double.MAX_VALUE);
		d[x] = 0;
		PriorityQueue<Node> que = new PriorityQueue<Node>();
		Node first = new Node(x, 0);
		que.add(first);

		while (!que.isEmpty()) {
			Node cur = que.poll();
			for (Hen hen : list.get(cur.v)) {
				double alt = d[cur.v] + hen.c;
				if (alt < d[hen.v]) {
					d[hen.v] = alt;
					Node next = new Node(hen.v, alt);
					que.add(next);
				}
			}
		}
		System.out.println(d[y]);
	}

	static class Hen {
		int v;
		double c;

		public Hen(int v, double c) {
			this.v = v;
			this.c = c;
		}
	}

	static class Node implements Comparable<Node> {
		int v;
		double d;

		public Node(int v, double d) {
			this.v = v;
			this.d = d;
		}

		public int compareTo(Node o) {
			return Double.compare(d, o.d);
		}
	}
}
0