結果

問題 No.748 yuki国のお財布事情
ユーザー rpy3cpprpy3cpp
提出日時 2019-05-10 22:11:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,415 bytes
コンパイル時間 1,726 ms
コンパイル使用メモリ 175,892 KB
実行使用メモリ 5,704 KB
最終ジャッジ日時 2023-09-14 17:59:30
合計ジャッジ時間 8,749 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 1 ms
4,380 KB
testcase_02 RE -
testcase_03 WA -
testcase_04 AC 2 ms
4,376 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 WA -
testcase_24 AC 2 ms
4,376 KB
testcase_25 RE -
testcase_26 AC 43 ms
5,240 KB
testcase_27 RE -
testcase_28 RE -
権限があれば一括ダウンロードができます

ソースコード

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(N, 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