結果

問題 No.168 ものさし
ユーザー syake_tasyake_ta
提出日時 2018-08-31 18:08:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,606 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 73,184 KB
実行使用メモリ 11,368 KB
最終ジャッジ日時 2023-10-11 22:52:29
合計ジャッジ時間 2,958 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
7,648 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 3 ms
4,372 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 31 ms
7,516 KB
testcase_12 AC 99 ms
10,024 KB
testcase_13 AC 140 ms
11,296 KB
testcase_14 AC 139 ms
11,236 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 4 ms
4,372 KB
testcase_18 AC 6 ms
4,680 KB
testcase_19 AC 66 ms
11,140 KB
testcase_20 AC 66 ms
11,368 KB
testcase_21 AC 68 ms
11,252 KB
testcase_22 AC 69 ms
11,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const int INF = (int)1e9 + 1;

int n;
ll x[1010], y[1010];
ll d[1010][1010];

class DisjointSet {
public:
	vector<int> rank, p;
	
	DisjointSet() {}
	DisjointSet(int size) {
		rank.resize(size, 0);
		p.resize(size, 0);
		for (int i = 0; i < size; i++) {
			makeSet(i);
		}
	}

	void makeSet(int x) {
		p[x] = x;
		rank[x] = 0;
	}

	bool same(int x, int y) {
		return findSet(x) == findSet(y);
	}

	void unite(int x, int y) {
		link(findSet(x), findSet(y));
	}

	void link(int x, int y) {
		if (rank[x] > rank[y]) {
			p[y] = x;
		}
		else {
			p[x] = y;
			if (rank[x] == rank[y]) {
				rank[y]++;
			}
		}
	}

	int findSet(int x) {
		if (x != p[x]) {
			p[x] = findSet(p[x]);
		}
		return p[x];
	}
};

bool solve(ll len) {
	DisjointSet ds = DisjointSet(n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (d[i][j] <= len * len) {
				ds.unite(i, j);
			}
		}
	}
	return ds.same(0, n - 1);
}

int main(void) {
	cin >> n;
	
	for (int i = 0; i < n; i++) {
		cin >> x[i] >> y[i];
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			d[i][j] = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
		}
	}

	ll left = 0LL, right = 1e10;
	while (right - left > 1) {
		ll mid = (left + right) / 2;
		if (solve(mid)) {
			right = mid;
		}
		else {
			left = mid;
		}
	}

	ll ans = left;
	for (int i = 0; i <= 11; i++) {
		if (ans % 10 == 0) {
			if (solve(ans)) break;
		}
		ans++;
	}

	cout << ans << '\n';
	return 0;
}
0