結果

問題 No.748 yuki国のお財布事情
ユーザー merom686merom686
提出日時 2018-10-19 22:14:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 82,692 KB
実行使用メモリ 4,652 KB
最終ジャッジ日時 2023-08-12 01:13:17
合計ジャッジ時間 2,719 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 19 ms
4,380 KB
testcase_17 AC 37 ms
4,384 KB
testcase_18 AC 42 ms
4,568 KB
testcase_19 AC 48 ms
4,652 KB
testcase_20 AC 42 ms
4,380 KB
testcase_21 AC 41 ms
4,560 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 35 ms
4,384 KB
testcase_26 AC 41 ms
4,588 KB
testcase_27 AC 39 ms
4,560 KB
testcase_28 AC 32 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct UnionFind {
    UnionFind(int n) : p(n, -1) {}
    int root(int u) {
        return p[u] < 0 ? u : p[u] = root(p[u]);
    }
    bool same(int u, int v) {
        return root(u) == root(v);
    }
    void unite(int u, int v) {
        u = root(u);
        v = root(v);
        if (u == v) return;
        if (p[u] > p[v]) swap(u, v);
        p[u] += p[v];
        p[v] = u;
    }
    vector<int> p;
};

struct Edge {
    bool operator<(Edge o) { return c < o.c; }
    int i, j;
    int c;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m, k;
    cin >> n >> m >> k;

    ll r = 0;
    vector<Edge> e(m);
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        e[i] = { a, b, c };
        r += e[i].c;
    }

    UnionFind uf(n);
    for (int j = 0; j < k; j++) {
        int i;
        cin >> i;
        i--;
        uf.unite(e[i].i, e[i].j);
        r -= e[i].c;
    }

    sort(e.begin(), e.end());
    for (int i = 0; i < m; i++) {
        if (uf.same(e[i].i, e[i].j)) continue;
        uf.unite(e[i].i, e[i].j);
        r -= e[i].c;
    }

    cout << r << endl;

    return 0;
}
0