結果

問題 No.748 yuki国のお財布事情
ユーザー face4face4
提出日時 2018-10-19 22:18:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 1,045 ms
コンパイル使用メモリ 89,352 KB
実行使用メモリ 9,552 KB
最終ジャッジ日時 2024-04-29 16:07:29
合計ジャッジ時間 3,464 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 30 ms
5,376 KB
testcase_15 AC 20 ms
5,376 KB
testcase_16 AC 59 ms
5,376 KB
testcase_17 AC 111 ms
7,380 KB
testcase_18 AC 136 ms
8,400 KB
testcase_19 AC 160 ms
9,552 KB
testcase_20 AC 144 ms
8,912 KB
testcase_21 AC 130 ms
8,020 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 96 ms
6,608 KB
testcase_26 AC 119 ms
7,756 KB
testcase_27 AC 113 ms
7,500 KB
testcase_28 AC 110 ms
7,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;

typedef long long ll;

struct Edge{
    int from , to, cost;
    Edge() {}
    Edge(int f, int t, int c) : from(f), to(t), cost(c) {}
    bool operator<(const Edge other) const{
        return cost < other.cost;
    }
};

class DisjointSet{
  public:
    vector<int> rank, p;

    DisjointSet(){}
    DisjointSet(int size){
        rank.resize(size, 0);
        p.resize(size, 0);
        for(int i = 0; i < size; i++) makeSet(i);
    }

    void makeSet(int x){
        p[x] = x;
        rank[x] = 0;
    }

    bool same(int x, int y){
        return findSet(x) == findSet(y);
    }
    
    void unite(int x, int y){
        link(findSet(x), findSet(y));
    }

    void link(int x, int y){
        if(rank[x] > rank[y]){
            p[y] = x;
        }else{
            p[x] = y;
            if(rank[x] == rank[y]){
                rank[y]++;
            }
        }
    }

    int findSet(int x){
        if(x != p[x]){
            // path compression
            p[x] = findSet(p[x]);
        }
        return p[x];
    }
};

int main(){
    int n, m, k, a, b, c, e;

    cin >> n >> m >> k;

    vector<Edge> v;
    for(int i = 0; i < m; i++){
        cin >> a >> b >> c;
        a--, b--;
        v.push_back(Edge(a, b, c));
    }

    set<int> save;
    for(int i = 0; i < k; i++){
        cin >> e;
        e--;
        save.insert(e);
    }

    DisjointSet uf(n);

    vector<Edge> mst;
    for(int i = 0; i < m; i++){
        if(save.count(i) == 0)  mst.push_back(v[i]);
        else                    uf.unite(v[i].from, v[i].to);
    }

    sort(mst.begin(), mst.end());

    ll ans = 0;
    for(int i = 0; i < mst.size(); i++){
        if(!uf.same(mst[i].from, mst[i].to)){
            uf.unite(mst[i].from, mst[i].to);
        }else{
            ans += mst[i].cost;
        }
    }

    cout << ans << endl;

    return 0;
}
0