結果

問題 No.748 yuki国のお財布事情
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-10 22:13:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 1,415 bytes
コンパイル時間 1,706 ms
コンパイル使用メモリ 175,536 KB
実行使用メモリ 5,444 KB
最終ジャッジ日時 2023-09-14 17:59:42
合計ジャッジ時間 3,576 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 19 ms
4,380 KB
testcase_17 AC 39 ms
4,936 KB
testcase_18 AC 44 ms
5,052 KB
testcase_19 AC 49 ms
5,444 KB
testcase_20 AC 43 ms
5,260 KB
testcase_21 AC 44 ms
5,108 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 36 ms
4,580 KB
testcase_26 AC 42 ms
5,180 KB
testcase_27 AC 41 ms
4,852 KB
testcase_28 AC 33 ms
4,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M, K;
    cin >> N >> M >> K;
    vector<pair<int, int>> edges(M, pair<int, int>());
    vector<pair<int, int>> cs(M, pair<int, int>());
    vector<int> es(K, 0);
    long long total_cost = 0;

    for (int i = 0; i < M; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        edges[i] = make_pair(a-1, b-1);
        cs[i] = make_pair(c, i);
        total_cost += c;
    }
    for (auto &e: es) cin >> e;
    UnionFind uf(N);
    long long cost = 0;
    for (auto e: es){
        cost += cs[e - 1].first;
        uf.unite(edges[e-1].first, edges[e-1].second);
    }
    sort(cs.begin(), cs.end());
    for (auto &ci: cs){
        int i = ci.second;
        int a = edges[i].first;
        int b = edges[i].second;
        if (uf.root(a) == uf.root(b)) continue;
        cost += ci.first;
        uf.unite(a, b);
    }
    cout << total_cost - cost << endl;
    return 0;
}
0