結果

問題 No.748 yuki国のお財布事情
ユーザー roarisroaris
提出日時 2019-11-21 19:43:21
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,967 bytes
コンパイル時間 2,160 ms
コンパイル使用メモリ 137,052 KB
実行使用メモリ 11,668 KB
最終ジャッジ日時 2023-08-20 13:25:49
合計ジャッジ時間 4,989 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,360 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 17 ms
5,192 KB
testcase_17 AC 39 ms
9,528 KB
testcase_18 AC 41 ms
8,772 KB
testcase_19 AC 41 ms
8,188 KB
testcase_20 AC 39 ms
7,452 KB
testcase_21 AC 42 ms
9,088 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 38 ms
9,336 KB
testcase_26 AC 45 ms
10,936 KB
testcase_27 AC 46 ms
11,668 KB
testcase_28 AC 27 ms
5,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef tuple<int, int, int> T;

struct Unionfind {
    vector<int> par, rank;
    
    Unionfind(int n) {
        par.resize(n);
        fill(par.begin(), par.end(), -1);
        rank.resize(n);
        fill(rank.begin(), rank.end(), 0);
    }
    
    int root(int x) {
        if (par[x]<0) return x;
        par[x] = root(par[x]);
        return par[x];
    }
    
    void unite(int x, int y) {
        int rx = root(x), ry = root(y);
        if (rx==ry) return;
        
        if (rank[rx]>=rank[ry]) {
            par[rx] += par[ry];
            par[ry] = rx;
            
            if (rank[rx]==rank[ry]) {
                rank[rx]++;
            }
        }
        else {
            par[ry] += par[rx];
            par[rx] = ry;
        }
    }
    
    int is_same(int x, int y) {
        return root(x)==root(y);
    }
    
    int count(int x) {
        return -par[root(x)];
    }
};

signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    int N, M, K; cin >> N >> M >> K;
    int a[M], b[M], c[M];
    int cost_sum = 0;
    
    for (int i=0; i<M; i++) {
        cin >> a[i] >> b[i] >> c[i];
        cost_sum += c[i];
    }
    
    int flag[M];
    fill(flag, flag+M, 0);
    
    for (int i=0; i<K; i++) {
        int ei; cin >> ei;
        flag[ei-1] = 1;
    }
    
    vector<T> edges;
    Unionfind uf = Unionfind(N);
    int cost = 0;
    
    for (int i=0; i<M; i++) {
        if (flag[i]) {
            uf.unite(a[i]-1, b[i]-1);
            cost += c[i];
        }
        else {
            edges.push_back(make_tuple(a[i]-1, b[i]-1, c[i]));
        }
    }
    
    sort(edges.begin(), edges.end(), [](T const t1, T const t2) {return get<2>(t1)<get<2>(t2);});
    
    for (auto& [ai, bi, ci] : edges) {
        if (!(uf.is_same(ai, bi))) {
            uf.unite(ai, bi);
            cost += ci;
        }
    }
    
    cout << cost_sum-cost << endl;
}
0