結果

問題 No.1473 おでぶなおばけさん
ユーザー firiexpfiriexp
提出日時 2021-04-28 09:12:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,179 bytes
コンパイル時間 1,657 ms
コンパイル使用メモリ 115,316 KB
実行使用メモリ 10,744 KB
最終ジャッジ日時 2023-09-21 10:02:19
合計ジャッジ時間 9,318 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 66 ms
9,932 KB
testcase_03 AC 51 ms
8,748 KB
testcase_04 AC 35 ms
7,368 KB
testcase_05 AC 13 ms
4,376 KB
testcase_06 AC 54 ms
8,624 KB
testcase_07 AC 70 ms
10,744 KB
testcase_08 AC 73 ms
10,472 KB
testcase_09 AC 72 ms
10,508 KB
testcase_10 AC 38 ms
4,876 KB
testcase_11 AC 38 ms
4,808 KB
testcase_12 AC 38 ms
4,868 KB
testcase_13 AC 26 ms
4,792 KB
testcase_14 AC 18 ms
4,380 KB
testcase_15 AC 34 ms
4,792 KB
testcase_16 AC 37 ms
4,748 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 31 ms
5,056 KB
testcase_20 AC 43 ms
8,116 KB
testcase_21 AC 43 ms
5,960 KB
testcase_22 AC 51 ms
9,012 KB
testcase_23 AC 44 ms
8,748 KB
testcase_24 AC 41 ms
7,816 KB
testcase_25 AC 53 ms
8,736 KB
testcase_26 AC 57 ms
8,676 KB
testcase_27 AC 20 ms
4,784 KB
testcase_28 AC 68 ms
10,592 KB
testcase_29 AC 38 ms
6,432 KB
testcase_30 AC 44 ms
6,904 KB
testcase_31 AC 63 ms
10,332 KB
testcase_32 AC 46 ms
8,748 KB
testcase_33 AC 39 ms
7,608 KB
testcase_34 AC 22 ms
5,640 KB
testcase_35 AC 22 ms
5,144 KB
testcase_36 AC 39 ms
6,300 KB
testcase_37 AC 44 ms
8,388 KB
testcase_38 AC 9 ms
4,376 KB
testcase_39 AC 38 ms
5,108 KB
testcase_40 AC 38 ms
4,876 KB
testcase_41 AC 35 ms
4,780 KB
testcase_42 AC 35 ms
5,096 KB
testcase_43 AC 40 ms
8,116 KB
testcase_44 AC 40 ms
7,892 KB
testcase_45 AC 40 ms
7,808 KB
testcase_46 AC 49 ms
7,872 KB
testcase_47 AC 61 ms
8,608 KB
testcase_48 AC 54 ms
8,424 KB
権限があれば一括ダウンロードができます

ソースコード

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