結果

問題 No.748 yuki国のお財布事情
ユーザー MisterMister
提出日時 2020-08-07 14:56:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,889 bytes
コンパイル時間 1,260 ms
コンパイル使用メモリ 84,468 KB
実行使用メモリ 5,556 KB
最終ジャッジ日時 2023-10-24 14:15:56
合計ジャッジ時間 3,176 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 11 ms
4,348 KB
testcase_15 AC 8 ms
4,348 KB
testcase_16 AC 20 ms
4,348 KB
testcase_17 AC 40 ms
5,048 KB
testcase_18 AC 45 ms
5,292 KB
testcase_19 AC 51 ms
5,556 KB
testcase_20 AC 45 ms
5,292 KB
testcase_21 AC 45 ms
5,292 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 37 ms
4,716 KB
testcase_26 AC 44 ms
5,556 KB
testcase_27 AC 43 ms
5,556 KB
testcase_28 AC 34 ms
4,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
using Edges = std::vector<Edge<Cost>>;

struct UnionFind {
    std::vector<int> par, sz;
    int gnum;

    explicit UnionFind(int n)
        : par(n), sz(n, 1), gnum(n) {
        std::iota(par.begin(), par.end(), 0);
    }

    int find(int v) {
        return (par[v] == v) ? v : (par[v] = find(par[v]));
    }

    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (sz[u] < sz[v]) std::swap(u, v);
        sz[u] += sz[v];
        par[v] = u;
        --gnum;
    }

    bool same(int u, int v) { return find(u) == find(v); }
    bool ispar(int v) { return v == find(v); }
    int size(int v) { return sz[find(v)]; }
};

using lint = long long;

void solve() {
    int n, m, k;
    std::cin >> n >> m >> k;

    Edges<lint> es(m);
    lint esum = 0;
    for (auto& e : es) {
        std::cin >> e.src >> e.dst >> e.cost;
        --e.src, --e.dst;
        esum += e.cost;
    }

    lint ans = 0;
    UnionFind uf(n);

    while (k--) {
        int ei;
        std::cin >> ei;
        const auto& e = es[--ei];

        ans += e.cost;
        uf.unite(e.src, e.dst);
    }

    std::sort(es.begin(), es.end());
    for (const auto& e : es) {
        if (uf.same(e.src, e.dst)) continue;

        ans += e.cost;
        uf.unite(e.src, e.dst);
    }

    std::cout << esum - ans << "\n";
}

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

    solve();

    return 0;
}
0