結果

問題 No.2200 Weird Shortest Path
ユーザー umimelumimel
提出日時 2023-02-11 02:25:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,618 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 178,420 KB
実行使用メモリ 9,332 KB
最終ジャッジ日時 2023-09-22 02:31:38
合計ジャッジ時間 9,312 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 29 ms
4,972 KB
testcase_18 AC 50 ms
6,720 KB
testcase_19 AC 84 ms
8,188 KB
testcase_20 AC 81 ms
8,400 KB
testcase_21 AC 33 ms
5,576 KB
testcase_22 AC 45 ms
6,076 KB
testcase_23 AC 88 ms
8,660 KB
testcase_24 AC 16 ms
4,380 KB
testcase_25 AC 82 ms
8,128 KB
testcase_26 AC 86 ms
8,404 KB
testcase_27 AC 31 ms
5,396 KB
testcase_28 AC 65 ms
7,200 KB
testcase_29 AC 86 ms
8,780 KB
testcase_30 AC 86 ms
8,536 KB
testcase_31 AC 53 ms
6,548 KB
testcase_32 AC 89 ms
9,332 KB
testcase_33 AC 88 ms
9,256 KB
testcase_34 AC 90 ms
9,256 KB
testcase_35 AC 90 ms
9,184 KB
testcase_36 AC 92 ms
9,308 KB
testcase_37 AC 91 ms
9,308 KB
testcase_38 AC 93 ms
9,260 KB
testcase_39 AC 48 ms
6,916 KB
testcase_40 AC 71 ms
9,288 KB
testcase_41 AC 43 ms
7,012 KB
testcase_42 AC 68 ms
9,244 KB
testcase_43 AC 97 ms
9,256 KB
testcase_44 AC 94 ms
9,260 KB
testcase_45 AC 95 ms
9,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

struct UnionFind{
    vector<ll> parent;
    vector<ll> sizes;
    
    UnionFind(ll N) : parent(N), sizes(N, 1){
        rep(i, N) parent[i] = i;
    }
    
    ll root(ll x){
        if (parent[x] == x) return x;
        return parent[x] = root(parent[x]);
    }
 
    void unite(ll x, ll y){
        ll rx = root(x);
        ll ry = root(y);
        if (rx == ry) return;
        if (sizes[rx] < sizes[ry]) swap(rx, ry);
        sizes[rx] += sizes[ry];
        parent[ry] = rx;
    }
 
    bool same(ll x, ll y){
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
 
    ll size(ll x){
        return sizes[root(x)];
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll n, m;
    cin >> n >> m;

    vector<tuple<ll, ll, ll>> edge(m);
    rep(i, m){
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        edge[i]={c, a, b};
    }

    sort(all(edge));

    UnionFind UF(n);
    ll ans = 0;
    rep(i, m){
        ll w = get<0>(edge[i]);
        ll u = get<1>(edge[i]);
        ll v = get<2>(edge[i]);

        if(!UF.same(u, v)){
            ans += w * UF.size(u)*UF.size(v);
            UF.unite(u, v);
        }
    }

    cout << ans << endl;
}
0