結果

問題 No.168 ものさし
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-02-25 11:30:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,071 bytes
コンパイル時間 707 ms
コンパイル使用メモリ 81,596 KB
実行使用メモリ 20,768 KB
最終ジャッジ日時 2023-08-25 21:00:23
合計ジャッジ時間 2,689 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
7,696 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 27 ms
9,392 KB
testcase_12 AC 85 ms
20,280 KB
testcase_13 AC 115 ms
20,556 KB
testcase_14 AC 116 ms
20,768 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 8 ms
4,380 KB
testcase_19 AC 136 ms
19,716 KB
testcase_20 AC 143 ms
19,632 KB
testcase_21 AC 143 ms
20,200 KB
testcase_22 AC 144 ms
19,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)

#define PI 3.1415926535

typedef long long ll;
//typedef pair<int, int> P;
//typedef complex<double> C;

struct edge {
    int from, to;
    ll cost;
    edge() {}
    edge(int f, int t, ll c) {
        from = f; to = t; cost = c;
    }
};

bool cmp (const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

const int MAX_N = 1000;

int N;
ll X[MAX_N], Y[MAX_N];
vector<edge> es;
int uni[MAX_N];

void input() {
    cin >> N;
    REP(i, N) cin >> X[i] >> Y[i];
}

ll hyp(int i, int j) {
    return (X[i] - X[j]) * (X[i] - X[j]) + (Y[i] - Y[j]) * (Y[i] - Y[j]);
}

void initEdge() {
    REP(i, N) {
        FOR(j, i + 1, N) {
            ll dist = (ll)sqrt(hyp(i, j));
            ll cost = dist - dist % 10;   // 辺(i, j)を書くのに必要なものさしの長さ
            while (cost * cost < hyp(i, j)) cost += 10;
            es.pb(edge(i, j, cost));
            es.pb(edge(j, i, cost));
        }
    }
}

void initUf(int n) {
    REP(i, n) uni[i] = -1;
}

int root(int x) {
    if (uni[x] < 0) return x;
    return uni[x] = root(uni[x]);
}

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

void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (uni[x] > uni[y]) swap(x, y);
    uni[x] += uni[y];
    uni[y] = x;
}

void solve() {
    initEdge();
    sort(ALL(es), cmp);
    initUf(N);
    REP(i, es.size()) {
        edge e = es[i];
        if (!same(e.from, e.to)) {
            unite(e.from, e.to);
        }
        if (same(0, N - 1)) {
            cout << e.cost << endl;
            return;
        }
    }
}

int main() {
    input();
    solve();
    return 0;
}
0