結果

問題 No.3490 最高経路問題
コンテスト
ユーザー GOTKAKO
提出日時 2026-04-03 21:28:34
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,294 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,570 ms
コンパイル使用メモリ 228,600 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-03 21:28:44
合計ジャッジ時間 2,749 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

class UnionFind{
    private:
    vector<int> par,siz;
    public:
    UnionFind(int N){
        par.resize(N,-1);
        siz.resize(N,1);
    }
 
    int root(int x){ //連結成分の代表頂点を返す.
        if(par.at(x) == -1) return x;
        else return par.at(x) = root(par.at(x));
    }
    bool unite(int u, int v){ //u,vを連結する 連結してた->false,した->trueを返す.
        u = root(u),v = root(v);
        if(u == v) return false;
 
        if(siz.at(u) < siz.at(v)) swap(u,v); //Union by size.
        par.at(v) = u;
        siz.at(u) += siz.at(v);
        return true;
    }
    bool issame(int u, int v){ //同じ連結成分ならtrue.
        if(root(u) == root(v)) return true;
        else return false;
    }
    int size(int pos){return siz.at(root(pos));} //posの連結成分の大きさを返す.
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M; cin >> N >> M;
    vector<tuple<int,int,int>> edge(M);
    for(auto &[c,a,b] : edge) cin >> a >> b >> c,a--,b--;
    sort(edge.rbegin(),edge.rend());
    UnionFind Z(N);
    
    for(auto [c,a,b] : edge){
        Z.unite(a,b);
        if(Z.issame(0,N-1)){cout << c << "\n"; return 0;}
    }
    cout << "NaN\n";
}
0