結果

問題 No.2200 Weird Shortest Path
ユーザー 鴨志田卓鴨志田卓
提出日時 2023-03-11 01:31:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 696 bytes
コンパイル時間 2,332 ms
コンパイル使用メモリ 211,444 KB
実行使用メモリ 8,232 KB
最終ジャッジ日時 2023-10-18 09:29:42
合計ジャッジ時間 10,258 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,444 KB
testcase_01 AC 2 ms
4,448 KB
testcase_02 AC 3 ms
4,448 KB
testcase_03 AC 3 ms
4,464 KB
testcase_04 AC 4 ms
4,524 KB
testcase_05 AC 3 ms
4,444 KB
testcase_06 AC 2 ms
4,452 KB
testcase_07 AC 3 ms
4,464 KB
testcase_08 AC 4 ms
4,488 KB
testcase_09 AC 3 ms
4,484 KB
testcase_10 AC 3 ms
4,448 KB
testcase_11 AC 3 ms
4,448 KB
testcase_12 AC 2 ms
4,448 KB
testcase_13 AC 2 ms
4,448 KB
testcase_14 AC 2 ms
4,448 KB
testcase_15 AC 3 ms
4,484 KB
testcase_16 AC 2 ms
4,448 KB
testcase_17 AC 68 ms
5,084 KB
testcase_18 AC 116 ms
5,804 KB
testcase_19 AC 208 ms
8,232 KB
testcase_20 AC 196 ms
8,232 KB
testcase_21 AC 74 ms
5,804 KB
testcase_22 AC 107 ms
5,804 KB
testcase_23 AC 211 ms
8,232 KB
testcase_24 AC 37 ms
5,084 KB
testcase_25 AC 201 ms
8,232 KB
testcase_26 AC 211 ms
8,232 KB
testcase_27 AC 73 ms
5,804 KB
testcase_28 AC 157 ms
8,232 KB
testcase_29 AC 211 ms
8,232 KB
testcase_30 AC 210 ms
8,232 KB
testcase_31 AC 128 ms
5,804 KB
testcase_32 AC 210 ms
8,232 KB
testcase_33 AC 211 ms
8,232 KB
testcase_34 AC 210 ms
8,232 KB
testcase_35 AC 213 ms
8,232 KB
testcase_36 AC 211 ms
8,232 KB
testcase_37 AC 211 ms
8,232 KB
testcase_38 AC 214 ms
8,232 KB
testcase_39 AC 105 ms
5,804 KB
testcase_40 AC 161 ms
8,232 KB
testcase_41 AC 92 ms
5,804 KB
testcase_42 AC 151 ms
8,232 KB
testcase_43 AC 223 ms
8,232 KB
testcase_44 AC 227 ms
8,232 KB
testcase_45 AC 224 ms
8,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using edge = tuple<int, int, int>;
const int N = 2e5 + 10;

int fa[N];

int find(int x) {
    return fa[x] > 0 ? find(fa[x]) : x;
}

int n, m;

vector<edge> e;

int main() {
    memset(fa, -1, sizeof fa);
    cin >> n >> m;
    for(int i = 1, u, v, w; i <= m; i ++) {
        cin >> u >> v >> w;
        e.push_back({w, u, v});
    }
    sort(e.begin(), e.end());
    long long ans = 0;
    for(auto &[w, u, v] : e) {
        int fu = find(u), fv = find(v);
        if(fu == fv) continue;
        ans += (long long)w * fa[fu] * fa[fv];
        if(fa[fu] < fa[fv]) swap(fu, fv);
        fa[fv] += fa[fu];
        fa[fu] = fv;
    }
    cout << ans;
}
0