結果

問題 No.748 yuki国のお財布事情
ユーザー kyunakyuna
提出日時 2019-07-20 14:08:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,784 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 81,292 KB
実行使用メモリ 9,392 KB
最終ジャッジ日時 2023-08-28 15:05:52
合計ジャッジ時間 3,531 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 25 ms
4,380 KB
testcase_15 AC 17 ms
4,380 KB
testcase_16 AC 48 ms
5,180 KB
testcase_17 AC 95 ms
5,668 KB
testcase_18 AC 113 ms
8,868 KB
testcase_19 AC 128 ms
9,392 KB
testcase_20 AC 115 ms
9,104 KB
testcase_21 AC 109 ms
9,072 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 87 ms
5,124 KB
testcase_26 AC 112 ms
8,688 KB
testcase_27 AC 104 ms
8,036 KB
testcase_28 AC 89 ms
8,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct UnionFind {
    vector<int> data;
    int __size;
    UnionFind(int size) : data(size, -1), __size(size) { }
    bool unionSet(int x, int y) {
        if ((x = root(x)) != (y = root(y))) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x; __size--;
        }
        return x != y;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return __size; }
};

template <typename T>
struct Edge { int src, dst; T cost;
    Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }
    Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }
};
template <typename T> using Edges = vector<Edge<T>>;
template<typename T>
pair<T, Edges<T>> kruskal(Edges<T> &edges, Edges<T> &fixed, int v) {
    sort(begin(edges), end(edges), [](const Edge<T> &a, const Edge<T> &b) {
        return a.cost < b.cost;
    });
    UnionFind uf(v);
    T total = 0; Edges<T> F;
    for (auto &e: fixed) {
        uf.unionSet(e.src, e.dst), total += e.cost, F.emplace_back(e);
    }
    for (auto &e: edges) {
        if (uf.unionSet(e.src, e.dst)) total += e.cost, F.emplace_back(e);
    }
    return {total, F};
}

int main() {
    int V, E, K; cin >> V >> E >> K;
    Edges<long long> edges, fixed;
    long long sum = 0;
    while (E--) {
        int s, t, w; cin >> s >> t >> w; s--, t--;
        edges.emplace_back(s, t, w);
        sum += w;
    }
    while (K--) {
        int e; cin >> e; e--;
        fixed.emplace_back(edges[e]);
    }
    cout << sum - kruskal(edges, fixed, V).first << endl;
    return 0;
}
0