結果

問題 No.748 yuki国のお財布事情
ユーザー HaarHaar
提出日時 2018-11-11 23:04:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,432 bytes
コンパイル時間 2,020 ms
コンパイル使用メモリ 180,312 KB
実行使用メモリ 8,076 KB
最終ジャッジ日時 2023-08-21 17:57:46
合計ジャッジ時間 4,735 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(int v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define LLI long long int
using namespace std;
template <typename T> using V = vector<T>;
template <typename T, typename U> using P = pair<T,U>;
template <typename T, typename U> P<T,U> operator+(const P<T,U> &a, const P<T,U> &b){return {a.first + b.first, a.second + b.second};}
template <typename T, typename U> P<T,U> operator-(const P<T,U> &a, const P<T,U> &b){return {a.first - b.first, a.second - b.second};}

class UnionFind{
  vector<int> parent, depth, size;
public:
  UnionFind(int n): parent(n), depth(n,1), size(n,1){REP(i, n) parent[i] = i;}
  int getRoot(int i){
    if(parent[i] == i) return i;
    else return parent[i] = getRoot(parent[i]);
  }
  bool isSame(int i, int j){return getRoot(i) == getRoot(j);}
  void merge(int i, int j){
    int ri = getRoot(i), rj = getRoot(j);
    if(ri != rj){
      if(depth[ri] < depth[rj]){
	parent[ri] = rj; size[rj] += size[ri];
      }else{
	parent[rj] = ri; size[ri] += size[rj];
	if(depth[ri] == depth[rj]) ++depth[ri];
      }
    }
  }
  int getSize(int i){return size[getRoot(i)];}
};

int N,M,K;
vector<int> e;

template <typename T>
vector<tuple<int,int,T>> kruskal(int n, vector<tuple<int,int,T>> &graph){
  UnionFind uf(n);
  REP(i,K) uf.merge(get<0>(graph[e[i]]), get<1>(graph[e[i]])); 
  vector<tuple<int,int,T>> mst;
  REP(i,K) mst.push_back(graph[e[i]]);
  sort(graph.begin(), graph.end(), [](tuple<int,int,T> &a, tuple<int,int,T> &b){return get<2>(a) < get<2>(b);});
  for(auto v : graph){
    int s,t,d;
    tie(s,t,d) = v;
    if(!uf.isSame(s,t)){
      uf.merge(s,t);
      mst.push_back(v);
    }
  }
  return mst;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M >> K;
  
  vector<tuple<int,int,int>> graph;
  int cost = 0;
  REP(i,M){
    int a,b,c; cin >> a >> b >> c;
    graph.push_back(make_tuple(a-1,b-1,c));
    cost += c;
  }

  e.resize(K);
  REP(i,K){cin >> e[i]; e[i]--;}

  vector<tuple<int,int,int>> res = kruskal(N, graph);

  int ans = 0;
  REP(i,res.size()){
    //cout << get<0>(res[i]) << " " << get<1>(res[i]) << " " << get<2>(res[i]) << endl;
    ans += get<2>(res[i]);
  }
  
  cout << cost-ans << endl;
  
  return 0;
}
0