結果

問題 No.168 ものさし
ユーザー keikei
提出日時 2018-09-07 01:40:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 738 ms / 2,000 ms
コード長 3,941 bytes
コンパイル時間 1,896 ms
コンパイル使用メモリ 186,776 KB
実行使用メモリ 21,012 KB
最終ジャッジ日時 2024-05-03 14:20:50
合計ジャッジ時間 6,132 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 246 ms
9,608 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 13 ms
6,940 KB
testcase_11 AC 231 ms
8,704 KB
testcase_12 AC 573 ms
20,644 KB
testcase_13 AC 472 ms
21,012 KB
testcase_14 AC 48 ms
20,460 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 10 ms
6,944 KB
testcase_18 AC 15 ms
6,940 KB
testcase_19 AC 738 ms
20,400 KB
testcase_20 AC 77 ms
20,184 KB
testcase_21 AC 555 ms
20,868 KB
testcase_22 AC 91 ms
20,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/168>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

struct UnionFind{
    vector<int> parent, rank;
    UnionFind(int n) : parent(n, -1), rank(n, 0) { }
    int find(int x){
        if(parent[x] == -1) return x;
        else return (parent[x] = find(parent[x]));
    }
    bool unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return false;
        if(rank[x] < rank[y])
            parent[x] = y;
        else
            parent[y] = x;
        if(rank[x] == rank[y]) rank[x]++;
        return true;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-9, pi = acos(-1.0);
namespace std {
    bool operator<(const Point &lhs, const Point &rhs) {
        if (lhs.real() < rhs.real() - eps) return true;
        if (lhs.real() > rhs.real() + eps) return false;
        return lhs.imag() < rhs.imag();
    }
}
Point input_point() {ld x, y; cin >> x >> y; return Point(x, y);} // 点の入力
bool eq(ld a, ld b) {return (abs(a - b) < eps);} // 誤差つき等号判定
ld dot(Point a, Point b) {return real(conj(a) * b);} // 内積
ld cross(Point a, Point b) {return imag(conj(a) * b);} // 外積


struct edge{
    int u,v;
    ld cost;
    edge(int u,int v,ld c):u(u),v(v),cost(c){}
    bool operator < (const edge& o) const{
        return cost < o.cost;
    }
    bool operator > (const edge& o) const{
        return cost > o.cost;
    }
};
ll solve(){
    ll res = 0;
    int N; cin >> N;
    vector<Point> ps(N);
    for(int i = 0; i < N;i++) ps[i] = input_point();
    priority_queue<edge,vector<edge>,greater<edge>> pq;
    for(int i = 0; i < N;i++){
        for(int j = i+1; j < N;j++){
            pq.push(edge(i,j,abs(ps[i]-ps[j])));
        }
    }
    
    UnionFind UF(N);
    while(pq.size()){
        auto e = pq.top(); pq.pop();
        //cout << e.u << " " << e.v << " " << e.cost << endl;
        if(UF.same(e.u,e.v)) continue;
        UF.unite(e.u,e.v);
        if(UF.same(0,N-1)) break;
    }
    
    
    vector<int> group;
    for(int i = 0; i < N;i++){
        if(UF.same(0,i)) group.push_back(i);
    }
    
    vector<ld> maxLen(N,LINF);
    queue<int> q; q.push(0); maxLen[0] = 0;
    while(q.size()){
        ll n = q.front(); q.pop();
        for(auto next:group){
            if(n == next) continue;
            ld len = abs(ps[n]-ps[next]);
            if(maxLen[next] > max(maxLen[n],len)){
                maxLen[next] = max(maxLen[n],len);
                q.push(next);
            }
        }
    }
    ld maxL = maxLen[N-1];
    res = ceil(maxL/10)*10;
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}
0