結果

問題 No.748 yuki国のお財布事情
ユーザー finefine
提出日時 2018-10-20 01:27:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,854 bytes
コンパイル時間 1,770 ms
コンパイル使用メモリ 174,916 KB
実行使用メモリ 5,256 KB
最終ジャッジ日時 2023-08-12 03:58:20
合計ジャッジ時間 3,867 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 10 ms
4,384 KB
testcase_15 AC 7 ms
4,384 KB
testcase_16 AC 18 ms
4,380 KB
testcase_17 AC 36 ms
5,056 KB
testcase_18 AC 42 ms
5,124 KB
testcase_19 AC 46 ms
5,116 KB
testcase_20 AC 43 ms
5,068 KB
testcase_21 AC 40 ms
5,072 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 36 ms
5,120 KB
testcase_26 AC 40 ms
5,256 KB
testcase_27 AC 38 ms
5,180 KB
testcase_28 AC 31 ms
5,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int size(int x) {
        return -data[find(x)];
    }
};

struct Edge {
    int u, v;
    ll cost;
    bool operator<(const Edge& e) const {
        return cost < e.cost;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, k;
    cin >> n >> m >> k;
    
    vector<Edge> es;
    ll ans = 0;
    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--; b--;
        es.push_back({a, b, c});
        ans += c;
    }

    UnionFind uf(n);
    for (int i = 0; i < k; i++) {
        int e;
        cin >> e;
        e--;
        uf.unite(es[e].u, es[e].v);
        ans -= es[e].cost;
    }

    sort(es.begin(), es.end());
    for (Edge& e : es) {
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            ans -= e.cost;
        }
    }
    cout << ans << endl;
    return 0;
}
0