結果

問題 No.1473 おでぶなおばけさん
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-04-11 17:46:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 1,921 bytes
コンパイル時間 2,551 ms
コンパイル使用メモリ 179,544 KB
実行使用メモリ 11,228 KB
最終ジャッジ日時 2023-09-09 19:55:08
合計ジャッジ時間 8,624 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 127 ms
10,116 KB
testcase_03 AC 99 ms
8,836 KB
testcase_04 AC 65 ms
7,528 KB
testcase_05 AC 24 ms
4,480 KB
testcase_06 AC 110 ms
8,896 KB
testcase_07 AC 129 ms
11,016 KB
testcase_08 AC 132 ms
10,936 KB
testcase_09 AC 131 ms
11,228 KB
testcase_10 AC 86 ms
4,684 KB
testcase_11 AC 86 ms
4,888 KB
testcase_12 AC 86 ms
4,568 KB
testcase_13 AC 54 ms
4,376 KB
testcase_14 AC 39 ms
4,376 KB
testcase_15 AC 73 ms
4,376 KB
testcase_16 AC 85 ms
4,632 KB
testcase_17 AC 7 ms
4,376 KB
testcase_18 AC 12 ms
4,380 KB
testcase_19 AC 63 ms
5,104 KB
testcase_20 AC 78 ms
8,056 KB
testcase_21 AC 90 ms
6,472 KB
testcase_22 AC 85 ms
9,056 KB
testcase_23 AC 70 ms
8,876 KB
testcase_24 AC 71 ms
8,036 KB
testcase_25 AC 104 ms
8,908 KB
testcase_26 AC 109 ms
8,888 KB
testcase_27 AC 44 ms
4,832 KB
testcase_28 AC 118 ms
10,680 KB
testcase_29 AC 86 ms
6,780 KB
testcase_30 AC 101 ms
7,328 KB
testcase_31 AC 106 ms
10,376 KB
testcase_32 AC 91 ms
8,788 KB
testcase_33 AC 75 ms
7,796 KB
testcase_34 AC 42 ms
5,748 KB
testcase_35 AC 46 ms
5,256 KB
testcase_36 AC 80 ms
6,540 KB
testcase_37 AC 80 ms
8,316 KB
testcase_38 AC 19 ms
4,376 KB
testcase_39 AC 86 ms
4,564 KB
testcase_40 AC 86 ms
4,656 KB
testcase_41 AC 76 ms
4,600 KB
testcase_42 AC 75 ms
4,760 KB
testcase_43 AC 87 ms
8,208 KB
testcase_44 AC 87 ms
8,208 KB
testcase_45 AC 88 ms
8,048 KB
testcase_46 AC 92 ms
7,824 KB
testcase_47 AC 114 ms
8,964 KB
testcase_48 AC 98 ms
8,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.04.11 17:45:59
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(int n) {
        par.resize(n, -1);
    }
    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }
    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }
    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int size(int x) {
        return -par[root(x)];
    }
};
int main() {
    int n;cin >> n;
    int m;cin >> m;
    vector<int> s(m),t(m),d(m),idx(m);
    iota(idx.begin(), idx.end(),0);
    for (int i = 0; i < m; i++) {
        cin >> s[i] >> t[i] >> d[i];
        s[i]--;
        t[i]--;
    }
    sort(idx.begin(), idx.end(), [&](int a,int b){
        return d[a] > d[b];
    });
    UnionFind uf(n);
    vector<vector<int>> g(n);
    for (int i = 0; i < m; i++) {
        int e = idx[i];
        while(i < m && d[e] == d[idx[i]]) {
            uf.unite(s[idx[i]],t[idx[i]]);
            g[s[idx[i]]].push_back(t[idx[i]]);
            g[t[idx[i]]].push_back(s[idx[i]]);
            i++;
        }
        i--;
        if (!uf.same(0,n-1)) continue;
        cout << d[e] << " ";
        queue<int> que;
        vector<int> dist(n,-1);
        que.push(0);
        dist[0] = 0;
        while(!que.empty()) {
            int q = que.front(); que.pop();
            for(int v : g[q]) {
                if (dist[v] != -1) continue;
                dist[v] = dist[q] + 1;
                que.push(v);
            }
        }
        cout << dist[n-1] << endl;
        break;
    }
    return 0;
}
0