結果

問題 No.2200 Weird Shortest Path
ユーザー didi just isdidi just is
提出日時 2023-03-23 00:44:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 1,695 ms
コンパイル使用メモリ 172,380 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2023-10-18 19:12:11
合計ジャッジ時間 5,238 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,780 KB
testcase_01 AC 2 ms
7,780 KB
testcase_02 AC 2 ms
7,784 KB
testcase_03 AC 2 ms
7,784 KB
testcase_04 AC 3 ms
7,784 KB
testcase_05 AC 2 ms
7,780 KB
testcase_06 AC 3 ms
7,784 KB
testcase_07 AC 3 ms
7,784 KB
testcase_08 AC 3 ms
7,784 KB
testcase_09 AC 3 ms
7,784 KB
testcase_10 AC 3 ms
7,784 KB
testcase_11 AC 2 ms
7,784 KB
testcase_12 AC 3 ms
7,784 KB
testcase_13 AC 3 ms
7,780 KB
testcase_14 AC 2 ms
7,784 KB
testcase_15 AC 3 ms
7,784 KB
testcase_16 AC 3 ms
7,784 KB
testcase_17 AC 26 ms
7,992 KB
testcase_18 AC 43 ms
10,368 KB
testcase_19 AC 74 ms
12,112 KB
testcase_20 AC 70 ms
12,436 KB
testcase_21 AC 29 ms
10,380 KB
testcase_22 AC 40 ms
10,176 KB
testcase_23 AC 74 ms
12,368 KB
testcase_24 AC 14 ms
7,932 KB
testcase_25 AC 71 ms
12,240 KB
testcase_26 AC 74 ms
12,292 KB
testcase_27 AC 27 ms
8,216 KB
testcase_28 AC 56 ms
10,248 KB
testcase_29 AC 76 ms
12,428 KB
testcase_30 AC 75 ms
12,256 KB
testcase_31 AC 46 ms
10,256 KB
testcase_32 AC 77 ms
12,544 KB
testcase_33 AC 76 ms
12,544 KB
testcase_34 AC 76 ms
12,544 KB
testcase_35 AC 76 ms
12,544 KB
testcase_36 AC 76 ms
12,544 KB
testcase_37 AC 76 ms
12,544 KB
testcase_38 AC 76 ms
12,544 KB
testcase_39 AC 39 ms
10,496 KB
testcase_40 AC 53 ms
12,544 KB
testcase_41 AC 35 ms
10,496 KB
testcase_42 AC 50 ms
12,544 KB
testcase_43 AC 65 ms
12,544 KB
testcase_44 AC 66 ms
12,544 KB
testcase_45 AC 65 ms
12,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N=2e5+10;
int n,m;
int f[N];
struct P{
    int a,b,c;
    bool operator<(const P&x){
        return c<x.c;
    }
}g[N];
struct DSU{
    int f[N];//祖先
    int S[N];//大小
    void init(int n){
        for(int i=1;i<=n;i++) f[i]=i,S[i]=1;
    }
    inline int find(int x){
        return (x==f[x])?x:f[x]=find(f[x]);
    }
    inline int size(int a){
        return S[find(a)];
    }
    bool same(int a,int b){
        return find(a)==find(b);
    }
    void merge(int a,int b){//a合并到b上
        a=find(a),b=find(b);
        if(a==b) return ;
        if(S[a]>S[b]) swap(a,b);
        S[b]+=S[a];
        f[a]=b;
    }
}dsu;
signed main(){

    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    //freopen("in.txt","r",stdin);

    cin>>n>>m;
    dsu.init(n);
    for(int i=0;i<m;i++){
        int a,b,c; cin>>a>>b>>c;
        g[i]={a,b,c};
    }
    sort(g,g+m);
    int ans=0;
    for(int i=0;i<m;i++){
        int a=g[i].a,b=g[i].b,c=g[i].c;
        if(dsu.same(a,b)) continue;
        ans+=dsu.size(a)*dsu.size(b)*c;
        dsu.merge(a,b);
    }
    cout<<ans;
    return 0;
}
0