結果

問題 No.748 yuki国のお財布事情
ユーザー PachicobuePachicobue
提出日時 2018-10-19 22:08:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 2,080 bytes
コンパイル時間 3,638 ms
コンパイル使用メモリ 214,808 KB
実行使用メモリ 12,320 KB
最終ジャッジ日時 2023-08-12 01:03:22
合計ジャッジ時間 6,284 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,388 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 15 ms
4,380 KB
testcase_14 AC 30 ms
4,380 KB
testcase_15 AC 18 ms
4,380 KB
testcase_16 AC 61 ms
5,752 KB
testcase_17 AC 107 ms
5,612 KB
testcase_18 AC 149 ms
8,980 KB
testcase_19 AC 201 ms
12,320 KB
testcase_20 AC 177 ms
11,564 KB
testcase_21 AC 138 ms
8,836 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 87 ms
4,644 KB
testcase_26 AC 117 ms
7,504 KB
testcase_27 AC 106 ms
7,116 KB
testcase_28 AC 134 ms
9,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//=================================
// Created on: 2018/10/19 21:58:40
//=================================
#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ll MOD = 1000000007LL;
template <typename T>
constexpr T INF = std::numeric_limits<T>::max() / 10;
std::mt19937 mt{std::random_device{}()};
class DisjointSets
{
public:
    DisjointSets(const std::size_t v) : parent(v), rank(v, 0), size(v, 1) { std::iota(parent.begin(), parent.end(), 0); }
    bool same(const std::size_t a, const std::size_t b) { return find(a) == find(b); }
    std::size_t find(const std::size_t a) { return parent[a] == a ? a : parent[a] = find(parent[a]); }
    void unite(std::size_t a, std::size_t b)
    {
        a = find(a), b = find(b);
        if (a == b) { return; }
        if (rank[a] < rank[b]) { std::swap(a, b); }
        if (rank[a] == rank[b]) { rank[a]++; }
        size[a] += size[b], parent[b] = a;
    }
    std::size_t getSize(const std::size_t a) { return size[find(a)]; }

private:
    std::vector<std::size_t> parent, rank, size;
};

int main()
{
    int N, M, K;
    std::cin >> N >> M >> K;
    using P = std::pair<int, int>;
    using PP = std::pair<ll, P>;
    std::vector<PP> edge(M);
    std::set<PP> e;
    DisjointSets uf(N);
    ll prev = 0;
    for (int i = 0; i < M; i++) {
        int a, b;
        ll c;
        std::cin >> a >> b >> c, a--, b--;
        edge[i] = {c, {a, b}}, prev += c;
    }
    ll sum = 0;
    for (int i = 0; i < K; i++) {
        int E;
        std::cin >> E, E--;
        uf.unite(edge[E].second.first, edge[E].second.second), e.insert(edge[E]), sum += edge[E].first;
    }
    std::sort(edge.begin(), edge.end());
    for (int i = 0; i < M; i++) {
        if (e.find(edge[i]) != e.end()) { continue; }
        const int a = edge[i].second.first, b = edge[i].second.second, c = edge[i].first;
        if (not uf.same(a, b)) { uf.unite(a, b), sum += c; }
    }
    std::cout << prev - sum << std::endl;
    return 0;
}
0