結果

問題 No.748 yuki国のお財布事情
ユーザー kappybarkappybar
提出日時 2020-05-07 16:03:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 2,200 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 176,236 KB
実行使用メモリ 6,788 KB
最終ジャッジ日時 2023-09-16 07:55:11
合計ジャッジ時間 6,371 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 15 ms
4,376 KB
testcase_14 AC 26 ms
4,376 KB
testcase_15 AC 18 ms
4,376 KB
testcase_16 AC 48 ms
4,808 KB
testcase_17 AC 100 ms
6,668 KB
testcase_18 AC 116 ms
6,668 KB
testcase_19 AC 127 ms
6,700 KB
testcase_20 AC 113 ms
6,656 KB
testcase_21 AC 109 ms
6,608 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 88 ms
6,696 KB
testcase_26 AC 109 ms
6,764 KB
testcase_27 AC 109 ms
6,788 KB
testcase_28 AC 88 ms
6,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;

struct edge{

    int from,to;
    ll cost;
    
    edge(int cost,int from,ll to):cost(cost),from(from),to(to){}

    bool operator< (const edge &right){ return cost < right.cost;}
    bool operator> (const edge &right){ return cost > right.cost;}
    bool operator<= (const edge &right){ return cost <= right.cost;}
    bool operator>= (const edge &right){ return cost >= right.cost;}
};

struct Unionfind{
    vector<int> parents;
    int m;
    Unionfind(int n):parents(n,-1){
        m = n;
    }

    int find(int x){
        if(parents.at(x) < 0){
            return x;
        }else{
            parents.at(x) = find(parents.at(x));
            return parents.at(x);
        }
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);

        if(x==y) return;
        if(parents.at(x) > parents.at(x))swap(x,y);
        parents.at(x) += parents.at(y);
        parents.at(y) = x;
    }  

   int size(int x){
       return -parents.at(find(x));
   }

   bool same(int x,int y){
       return find(x) ==find(y);
   }

   vector<int> members(int x){
       int root = find(x);
       vector<int> A;
       for(int i=0;i<m;i++){
           if(find(i) ==root ){
               A.push_back(i);
           }
       }
      return A;
  }
};

int main(){
    vector<edge> EG;

    int n,m,k;
    cin >> n >> m >>k;
    vector<int> a(m),b(m);
    vector<ll> c(m);
    ll sum = 0;
    rep(i,m){
        cin >> a[i] >> b[i] >> c[i];
        --a[i];--b[i];
        sum += c[i];
        EG.push_back(edge(c[i],a[i],b[i]));
    }
    Unionfind uf(n);
    ll res = 0;
    rep(i,k){
        int e;
        cin >> e;
        --e;
        uf.unite(a[e],b[e]);
        res += c[e];
        EG[e].cost = LINF;
    }
    sort(EG.begin(),EG.end());
    for(edge eg:EG){
        if(uf.same(eg.from,eg.to)) continue;
        uf.unite(eg.from,eg.to);
        res += eg.cost;
    }
    cout << sum - res << endl;
    return 0;
}
0