結果

問題 No.1473 おでぶなおばけさん
ユーザー firiexp
提出日時 2021-04-28 09:12:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 2,179 bytes
コンパイル時間 1,406 ms
コンパイル使用メモリ 113,324 KB
最終ジャッジ日時 2025-01-21 01:38:17
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |         scanf("%d %d %d", &s, &t, &d);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <typename T>
struct edge {
    int from, to;
    T cost;

    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}

    explicit operator int() const {return to;}
};

class UnionFind {
    vector<int> uni;
    int n;
public:
    explicit UnionFind(int n) : uni(static_cast<u32>(n), -1) , n(n){};

    int root(int a){
        if (uni[a] < 0) return a;
        else return (uni[a] = root(uni[a]));
    }

    bool unite(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] += uni[b];
        uni[b] = a;
        return true;
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<edge<int>> E;
    for (int i = 0; i < m; ++i) {
        int s, t, d;
        scanf("%d %d %d", &s, &t, &d);
        E.emplace_back(s-1, t-1, d);
    }
    sort(E.begin(),E.end(), [&](edge<int> &a, edge<int> &b){ return a.cost > b.cost; });
    UnionFind uf(n);
    int mx = INF<int>;
    for (int i = 0; i < m; ++i) {
        if(uf.root(0) == uf.root(n-1)) break;
        else {
            uf.unite(E[i].from, E[i].to);
            mx = E[i].cost;
        }
    }
    vector<vector<int>> G(n);
    for (int i = 0; i < m; ++i) {
        if(E[i].cost >= mx){
            G[E[i].from].emplace_back(E[i].to);
            G[E[i].to].emplace_back(E[i].from);
        }
    }
    vector<int> dist(n, INF<int>);
    queue<int> Q;
    dist[0] = 0;
    Q.emplace(0);
    while(!Q.empty()){
        int x = Q.front(); Q.pop();
        for (auto &&y : G[x]) {
            if(dist[y] == INF<int>) {
                dist[y] = dist[x]+1;
                Q.emplace(y);
            }
        }
    }
    printf("%d %d\n", mx, dist.back());
    return 0;
}
0