結果

問題 No.748 yuki国のお財布事情
ユーザー どららどらら
提出日時 2018-10-19 22:07:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 1,534 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 177,804 KB
実行使用メモリ 11,476 KB
最終ジャッジ日時 2023-08-12 01:02:30
合計ジャッジ時間 4,516 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 16 ms
4,376 KB
testcase_14 AC 27 ms
4,612 KB
testcase_15 AC 19 ms
4,528 KB
testcase_16 AC 50 ms
5,880 KB
testcase_17 AC 108 ms
10,992 KB
testcase_18 AC 117 ms
10,396 KB
testcase_19 AC 126 ms
9,400 KB
testcase_20 AC 111 ms
8,600 KB
testcase_21 AC 115 ms
10,052 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 100 ms
11,436 KB
testcase_26 AC 117 ms
11,476 KB
testcase_27 AC 115 ms
11,472 KB
testcase_28 AC 86 ms
8,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

class UnionFind{
  int n;
  vector<int> data;
public:
  UnionFind(int _n):n(_n),data(_n,-1){}
  bool link(int a, int b){
    int ra=find_root(a);
    int rb=find_root(b);
    if(ra!=rb){
      if(data[rb]<data[ra]) swap(ra,rb);
      data[ra]+=data[rb];
      data[rb]=ra;
    }
    return (ra!=rb);
  }
  bool check(int a, int b){
    return (find_root(a)==find_root(b));
  }
  int find_root(int a){
    return ((data[a]<0)?a:(data[a]=find_root(data[a])));
  }
};

struct ST {
  int a, b, c;
};
bool operator<(const ST& a, const ST& b){
  return a.c > b.c;
}

int main(){
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<int>> edges;
  rep(i,M){
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    edges.push_back({a,b,c});
  }
  
  UnionFind uf(N);
  ll ret = 0;
  rep(i,K){
    int e;
    cin >> e;
    e--;
    uf.link(edges[e][0], edges[e][1]);
    edges[e][0] = -1;
  }

  priority_queue<ST> que;
  rep(i,edges.size()){
    if(edges[i][0] == -1) continue;
    que.push((ST){edges[i][0], edges[i][1], edges[i][2]});
  }

  while(!que.empty()){
    ST st = que.top(); que.pop();
    if(uf.check(st.a, st.b)){
      ret += st.c;
    }else{
      uf.link(st.a, st.b);
    }
  }

  cout << ret << endl;
  
  return 0;
}

0