結果

問題 No.1473 おでぶなおばけさん
ユーザー milanis48663220
提出日時 2021-04-09 21:45:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 2,218 bytes
コンパイル時間 1,044 ms
コンパイル使用メモリ 102,844 KB
最終ジャッジ日時 2025-01-20 14:00:02
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;


struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    vector<int> s(m), t(m), d(m);
    for(int i = 0;i < m; i++){
        cin >> s[i] >> t[i] >> d[i];
        s[i]--; t[i]--;
    }
    int l = 1, r = (1e9)+1000;
    while(r-l > 1){
        UnionFind uf(n);
        int c = (r+l)/2;
        for(int i = 0; i < m; i++){
            if(d[i] >= c) uf.unionSet(s[i], t[i]);
        }
        if(uf.findSet(0, n-1)) l = c;
        else r = c;
    }

    vector<vector<int>> g(n);
    for(int i = 0; i < m; i++){
        if(d[i] >= l) {
            g[s[i]].push_back(t[i]);
            g[t[i]].push_back(s[i]);
        }
    }

    vector<bool> used(n, false);
    vector<int> dist(n);
    queue<int> que;
    que.push(0);
    dist[0] = 0;
    used[0] = true;
    while(!que.empty()){
        int v = que.front(); que.pop();
        for(int to : g[v]){
            if(!used[to]){
                used[to] = true;
                dist[to] = dist[v]+1;
                que.push(to);
            }
        }
    }
    cout << l << ' ' << dist[n-1] << endl;
}
0