結果

問題 No.748 yuki国のお財布事情
ユーザー kyunakyuna
提出日時 2019-07-20 14:07:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,772 bytes
コンパイル時間 784 ms
コンパイル使用メモリ 81,220 KB
実行使用メモリ 8,076 KB
最終ジャッジ日時 2023-08-28 15:05:48
合計ジャッジ時間 4,036 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 WA -
testcase_26 AC 104 ms
7,488 KB
testcase_27 WA -
testcase_28 AC 86 ms
7,332 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<int> edges, fixed;
    int 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