結果

問題 No.1473 おでぶなおばけさん
コンテスト
ユーザー addx
提出日時 2026-03-31 02:04:16
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 3,550 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,869 ms
コンパイル使用メモリ 389,072 KB
実行使用メモリ 12,156 KB
最終ジャッジ日時 2026-03-31 02:04:28
合計ジャッジ時間 11,849 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = unsigned __int128_t;
using mint = atcoder::static_modint<998244353>;
const int mod = 998244353;
#include <chrono>
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
template<class T>
bool chmin(T& a, const T& b){
    if (b < a){
        a = b;
        return true;
    }
    else { 
        return false;
    }
}
template<class T> 
bool chmax(T& a, const T& b){
    if (a < b){
        a = b;
        return true;
    }
    else {
        return false;
    }
}
struct Unionfind{
    vector<int> par, sz;
    vector<ll> w;
    Unionfind(int n) : par(n), sz(n, 1), w(n, 0) {
        iota(par.begin(), par.end(), 0);
    }
    int leader(int a){
        if (par[a] == a){
            return a;
        }
        int pa = par[a];
        int r = leader(pa);
        w[a] += w[pa];
        return par[a] = r;
    }
    bool same(int a, int b){
        return leader(a) == leader(b);
    }
    //w[b] - w[a] == cを満たさなかったら-1, 満たすならleaderを返す。
    int merge(int b, int a, ll c = 0){
        ll x = weight(b) - weight(a);
        a = leader(a);
        b = leader(b);
        if (a == b){
            if (c == x){
                return a;
            }
            else {
                return -1;
            }
        }
        c -= x;
        if (sz[a] < sz[b]){
            swap(a, b);
            c = -c;
        }
        sz[a] += sz[b];
        par[b] = a;
        w[b] = c;
        return a;
    }
    int size(int a){
        return sz[leader(a)]; 
    }
    vector<vector<int>> groups(){
        int n = par.size();
        vector<vector<int>> result(n), filtered;
        for (int i = 0; i < n; i++){
            int r = leader(i);
            result[r].push_back(i);
        }
        for (int i = 0; i < n; i++){
            if (!result[i].empty()){
                filtered.push_back(result[i]);
            }
        }
        return filtered;
    }
    ll weight(int a){
        leader(a);
        return w[a];
    }
    ll diff(int a, int b){
        return weight(b) - weight(a);
    }
};
void solve(){
    int n, m;
    cin >> n >> m;
    vector<array<int, 3>> arr;
    for (int i = 0; i < m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        u--;
        v--;
        arr.push_back({w, u, v});
    }
    sort(arr.rbegin(), arr.rend());
    Unionfind uf(n);
    vector<vector<int>> vec(n);
    int idx = 0, now = 2e9;
    while(1){
        now = arr[idx][0];
        while(idx < m && arr[idx][0] == now){
            vec[arr[idx][1]].push_back(arr[idx][2]);
            vec[arr[idx][2]].push_back(arr[idx][1]);
            uf.merge(arr[idx][1], arr[idx][2]);
            idx++;
        }
        if (uf.same(0, n - 1)){
            break;
        }
    }
    vector<int> dist(n, 1e9);
    queue<int> q;
    dist[0] = 0;
    q.push(0);
    while(!q.empty()){
        int x = q.front();
        q.pop();
        for (auto d : vec[x]){
            if (dist[d] == 1e9){
                dist[d] = dist[x] + 1;
                q.push(d);
            }
        }
    }
    cout << now << ' ' << dist[n - 1] << '\n';
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;   
    while(t--){
        cout << fixed << setprecision(15);          
        solve();
    }   
}
0