結果

問題 No.3490 最高経路問題
コンテスト
ユーザー 👑 Nachia
提出日時 2026-04-03 22:29:43
言語 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,565 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 695 ms
コンパイル使用メモリ 105,864 KB
実行使用メモリ 7,972 KB
最終ジャッジ日時 2026-04-03 22:30:09
合計ジャッジ時間 1,975 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
// disable assert
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template <class T> using V = vector<T>;
template <class A, class B> void chmax(A& l, const B& r){ if(l < r) l = r; }
template <class A, class B> void chmin(A& l, const B& r){ if(r < l) l = r; }


namespace nachia {

struct DsuFast{
private:
    std::vector<int> w;
public:
    DsuFast(int n = 0) : w(n, -1) {}
    int leader(int u){
        if(w[u] < 0) return u;
        return w[u] = leader(w[u]);
    }
    int operator[](int u){ return leader(u); }
    int merge(int u, int v){
        u = leader(u);
        v = leader(v);
        if(u == v) return u;
        if(-w[u] < -w[v]) std::swap(u, v);
        w[u] += w[v];
        w[v] = u;
        return u;
    }
    int size(int u){ return -w[leader(u)]; }
    bool same(int u, int v){ return leader(u) == leader(v); }
};

} // namespace nachia

void testcase(){
    ll N, M; cin >> N >> M;
    V<pair<ll, pair<ll,ll>>> E(M);
    REP(i,M){
        ll u,v,h; cin >> u >> v >> h;
        E[i] = { h, {u-1,v-1} };
    }
    sort(E.rbegin(), E.rend());
    auto dsu = nachia::DsuFast(N);
    for(auto [h,uv] : E){
        auto [u,v] = uv;
        dsu.merge(u,v);
        if(dsu.same(0, N-1)){ cout << h << "\n"; return; }
    }
    cout << "NaN\n";
}

int main(){
  cin.tie(0)->sync_with_stdio(0);
  testcase();
  return 0;
}
0