結果

問題 No.748 yuki国のお財布事情
ユーザー MisterMister
提出日時 2020-08-07 14:56:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,889 bytes
コンパイル時間 1,061 ms
コンパイル使用メモリ 82,620 KB
最終ジャッジ日時 2025-01-12 15:42:49
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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