結果

問題 No.748 yuki国のお財布事情
ユーザー risujirohrisujiroh
提出日時 2018-10-19 21:49:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,990 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 173,048 KB
実行使用メモリ 5,992 KB
最終ジャッジ日時 2023-08-12 00:49:13
合計ジャッジ時間 3,879 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 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,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 20 ms
4,376 KB
testcase_17 AC 38 ms
5,152 KB
testcase_18 AC 46 ms
5,536 KB
testcase_19 AC 51 ms
5,584 KB
testcase_20 AC 46 ms
5,316 KB
testcase_21 AC 46 ms
5,708 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 37 ms
5,308 KB
testcase_26 AC 45 ms
5,992 KB
testcase_27 AC 44 ms
5,736 KB
testcase_28 AC 35 ms
5,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }

struct QU {
  V<> par, rank, _size;

  QU(int n) : par(n), rank(n), _size(n, 1) { 
    iota(par.begin(), par.end(), 0);
  }

  int find(int a) {
    if (par[a] == a) return a;
    return par[a] = find(par[a]);
  }

  bool same(int a, int b) { return find(a) == find(b); }

  int size(int a) { return _size[find(a)]; }

  void unite(int a, int b) {
    a = find(a), b = find(b);
    if (a == b) return;
    if (rank[a] < rank[b]) {
      par[a] = b;
      _size[b] += _size[a];
    } else {
      par[b] = a;
      _size[a] += _size[b];
    }
    if (rank[a] == rank[b]) rank[a]++;
  }
};

template<class T> struct edge { int from, to; T w; edge(int a, int b, T w) : from(a), to(b), w(w) {} };

template<class T> T kruskal(V< edge<T> >& g, int n) {
  T res = 0;
  QU qu(n);
  sort(g.begin(), g.end(), [](const auto& a, const auto& b) { return a.w < b.w; });
  for (auto&& e : g) {
    if (!qu.same(e.from, e.to)) {
      qu.unite(e.from, e.to);
      res += e.w;
    }
  }
  return res;
}

int main() {
  cin.tie(NULL); ios::sync_with_stdio(false);
  int n, m, k; cin >> n >> m >> k;
  V< edge<lint> > g;
  for (int i = 0; i < m; i++) {
    int a, b, c; cin >> a >> b >> c, a--, b--;
    g.emplace_back(a, b, c);
  }
  QU qu(n);
  lint res = 0;
  for (int i = 0; i < k; i++) {
    int e; cin >> e, e--;
    res += g[e].w;
    qu.unite(g[e].from, g[e].to);
  }
  sort(g.begin(), g.end(), [](const auto& a, const auto& b) { return a.w < b.w; });
  lint s = 0;
  for (auto&& e : g) {
    s += e.w;
    if (!qu.same(e.from, e.to)) {
      res += e.w;
      qu.unite(e.from, e.to);
    }
  }
  cout << s - res << '\n';
}
0