結果

問題 No.748 yuki国のお財布事情
ユーザー PachicobuePachicobue
提出日時 2018-10-19 22:08:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 2,080 bytes
コンパイル時間 2,616 ms
コンパイル使用メモリ 216,800 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-04-29 15:53:10
合計ジャッジ時間 5,375 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 16 ms
5,376 KB
testcase_14 AC 33 ms
5,376 KB
testcase_15 AC 40 ms
5,376 KB
testcase_16 AC 67 ms
5,760 KB
testcase_17 AC 110 ms
5,760 KB
testcase_18 AC 158 ms
8,960 KB
testcase_19 AC 213 ms
12,288 KB
testcase_20 AC 190 ms
11,648 KB
testcase_21 AC 145 ms
9,088 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 92 ms
5,376 KB
testcase_26 AC 126 ms
7,680 KB
testcase_27 AC 114 ms
6,912 KB
testcase_28 AC 142 ms
9,728 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