結果

問題 No.748 yuki国のお財布事情
ユーザー mamekinmamekin
提出日時 2018-10-21 16:26:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 2,672 bytes
コンパイル時間 1,313 ms
コンパイル使用メモリ 124,576 KB
実行使用メモリ 5,532 KB
最終ジャッジ日時 2023-08-12 05:44:06
合計ジャッジ時間 4,024 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 24 ms
4,380 KB
testcase_15 AC 17 ms
4,380 KB
testcase_16 AC 46 ms
4,376 KB
testcase_17 AC 91 ms
4,652 KB
testcase_18 AC 109 ms
4,904 KB
testcase_19 AC 125 ms
5,532 KB
testcase_20 AC 110 ms
5,268 KB
testcase_21 AC 105 ms
5,308 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 83 ms
4,376 KB
testcase_26 AC 101 ms
5,532 KB
testcase_27 AC 100 ms
5,436 KB
testcase_28 AC 82 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

class UnionFindTree
{
private:
    int n;
    int groupNum;       // グループの数
    vector<int> parent; // 親ノード
    vector<int> rank;   // 木の高さの上限
    vector<int> num;    // グループの要素数
    int find(int i){
        if(parent[i] == i)
            return i;
        else
            return parent[i] = find(parent[i]);
    }
public:
    UnionFindTree(int n){ // コンストラクタ
        this->n = n;
        groupNum = n;
        parent.resize(n);
        for(int i=0; i<n; ++i)
            parent[i] = i;
        rank.assign(n, 0);
        num.assign(n, 1);
    }
    void unite(int a, int b){ // aとbのグループを併合
        if((a = find(a)) != (b = find(b))){
            if(rank[a] < rank[b]){
                parent[a] = b;
                num[b] += num[a];
            }
            else{
                parent[b] = a;
                if(rank[a] == rank[b])
                    ++ rank[a];
                num[a] += num[b];
            }
            -- groupNum;
        }
    }
    bool same(int a, int b){ // aとbのグループが同じかを調べる
        return find(a) == find(b);
    }
    int getNum(){ // グループの数を返す
        return groupNum;
    }
    int getNum(int a){ // aのグループの要素数を返す
        return num[find(a)];
    }
};

class Edge
{
public:
    int a, b, c;
    bool operator<(const Edge& e) const{
        return c < e.c;
    }
};

int main()
{
    int n, m, k;
    cin >> n >> m >> k;

    vector<Edge> edges(m);
    long long ans = 0;
    for(int i=0; i<m; ++i){
        cin >> edges[i].a >> edges[i].b >> edges[i].c;
        -- edges[i].a;
        -- edges[i].b;
        ans += edges[i].c;
    }

    UnionFindTree uft(n);
    for(int i=0; i<k; ++i){
        int x;
        cin >> x;
        -- x;
        uft.unite(edges[x].a, edges[x].b);
        ans -= edges[x].c;
    }

    sort(edges.begin(), edges.end());
    for(int i=0; i<m; ++i){
        int a = edges[i].a;
        int b = edges[i].b;
        if(!uft.same(a, b)){
            uft.unite(a, b);
            ans -= edges[i].c;
        }
    }
    cout << ans << endl;

    return 0;
}
0