結果

問題 No.168 ものさし
ユーザー togatogatogatoga
提出日時 2015-04-07 06:54:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 2,229 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 154,468 KB
実行使用メモリ 12,668 KB
最終ジャッジ日時 2023-08-25 20:55:36
合計ジャッジ時間 3,087 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
5,292 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 20 ms
5,284 KB
testcase_12 AC 60 ms
11,832 KB
testcase_13 AC 87 ms
11,824 KB
testcase_14 AC 87 ms
12,184 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 83 ms
12,284 KB
testcase_20 AC 84 ms
11,492 KB
testcase_21 AC 86 ms
12,668 KB
testcase_22 AC 85 ms
11,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long, long> pll;

const int INF = 1 << 29;
const double EPS = 1e-9;
const int MOD = 100000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
ll square(ll x) { return x * x; }
int N;
const int MAXN = 1010;
ll X[MAXN], Y[MAXN];
struct State {
  int src, tar;
  ll cost;
  State(int _src, int _tar, ll _cost) {
    src = _src;
    tar = _tar;
    cost = _cost;
  }
  bool operator<(const State &st) const { return cost > st.cost; }
};

ll check(ll n) {
  ll low = 0;
  ll high = 1LL << 32;
  while (high - low > 1) {
    ll med = (high + low) / 2;
    if (med * med <= n)
      low = med;
    else
      high = med;
  }
  return low;
}
//Union Find
struct UnionFind {
  vector<int> par;
  vector<int> rank;
  UnionFind(int n) {
    par.resize(n);
    rank.resize(n, 0);
    for (int i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  void unit(int x, int y) {
    int X = find(x);
    int Y = find(y);
    if (X == Y)
      return;
    if (rank[X] < rank[Y]) {
      par[X] = Y;
    } else {
      par[Y] = X;
      if (rank[X] == rank[Y]) {
        rank[X]++;
      }
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
};

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
  }
  priority_queue<State> que;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ll dist = check(square(X[i] - X[j]) + square(Y[i] - Y[j]));
      if (dist * dist < square(X[i] - X[j]) + square(Y[i] - Y[j])) {
        dist++;
      }
      if (dist % 10 == 0) {
        que.push(State(i, j, dist));
      } else {
        que.push(State(i, j, (dist / 10 + 1) * 10));
      }
    }
  }
  UnionFind uf(N);
  ll res = 0;
  while (1){
    if (uf.same(0, N - 1))break;

    State pos = que.top();
    que.pop();
    res = pos.cost;
    uf.unit(pos.src, pos.tar);
  }
  cout << res << endl;
  return 0;
}
0