結果

問題 No.168 ものさし
ユーザー kenkooookenkoooo
提出日時 2015-03-20 01:07:31
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,016 bytes
コンパイル時間 2,456 ms
コンパイル使用メモリ 75,228 KB
実行使用メモリ 71,676 KB
最終ジャッジ日時 2023-09-11 08:59:48
合計ジャッジ時間 5,485 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) {
		int N = nextInt();
		int[] x = new int[N];
		int[] y = new int[N];
		for (int i = 0; i < N; i++) {
			x[i] = nextInt();
			y[i] = nextInt();
		}

		PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
		UnionFind uFind = new UnionFind(N);

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < i; j++) {
				long dx = x[i] - x[j];
				long dy = y[i] - y[j];
				long distSq = dx * dx + dy * dy;
				long d = (long) Math.sqrt(distSq);
				while (distSq > d * d) {
					d++;
				}

				d = (d + 9) / 10 * 10;
				priorityQueue.add(new Edge(i, j, (int) d));
			}
		}

		int max = 0;
		while (!uFind.isSame(0, N - 1)) {
			Edge edge = priorityQueue.poll();
			if (!uFind.isSame(edge.from, edge.to)) {
				uFind.unite(edge.from, edge.to);
				max = Math.max(max, edge.weight);
			}
		}
		System.out.println(max);

	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
}

class UnionFind {
	int[] parts;

	UnionFind(int n) {
		parts = new int[n];
		for (int i = 0; i < n; i++)
			parts[i] = i;
	}

	public int find(int x) {
		if (parts[x] == x)
			return x;
		return parts[x] = find(parts[x]);
	}

	public Boolean isSame(int x, int y) {
		return find(x) == find(y);
	}

	public void unite(int x, int y) {
		if (find(x) == find(y))
			return;
		parts[find(x)] = find(y);
	}
}

class Edge implements Comparable<Edge> {
	int weight;
	int from, to;

	Edge(int w, int f, int t) {
		this.weight = w;
		this.from = f;
		this.to = t;
	}

	@Override
	public int compareTo(Edge edge) {
		// 昇順
		return this.weight - edge.weight;
	}
}
0