結果

問題 No.748 yuki国のお財布事情
ユーザー ikdikd
提出日時 2018-10-19 07:05:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,675 bytes
コンパイル時間 1,213 ms
コンパイル使用メモリ 85,648 KB
実行使用メモリ 7,560 KB
最終ジャッジ日時 2024-04-26 17:06:13
合計ジャッジ時間 3,749 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_06 AC 1 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 14 ms
5,376 KB
testcase_14 AC 26 ms
5,376 KB
testcase_15 AC 18 ms
5,376 KB
testcase_16 AC 50 ms
5,376 KB
testcase_17 AC 107 ms
6,984 KB
testcase_18 AC 121 ms
6,348 KB
testcase_19 AC 133 ms
6,220 KB
testcase_20 AC 116 ms
5,836 KB
testcase_21 AC 116 ms
6,476 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 97 ms
6,984 KB
testcase_26 AC 119 ms
7,500 KB
testcase_27 AC 116 ms
7,560 KB
testcase_28 AC 90 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

struct UnionFind {
  vector<int> par;
  vector<int> rank;
  UnionFind(int n) {
    par.resize(n);
    rep(i, n) par[i] = i;
    rank.resize(n, 0);
  }
  int find(int i) {
    if (i == par[i])
      return par[i];
    else
      return par[i] = find(par[i]);
  }
  void unite(int i, int j) {
    i = find(i), j = find(j);
    if (i == j) return;
    if (rank[i] < rank[j]) {
      par[i] = j;
    } else {
      par[j] = i;
      if (rank[i] == rank[j]) rank[i]++;
    }
  }
  bool same(int i, int j) { return find(i) == find(j); }
};

int main() {

  int n, m, k;
  cin >> n >> m >> k;

  struct Edge {
    int a, b, c;
  };
  vector<Edge> edges;
  rep(_, m) {
    int a, b, c;
    cin >> a >> b >> c;
    edges.push_back(Edge{a - 1, b - 1, c});
  }
  long long sum = 0;
  UnionFind uf(n);
  rep(_, k) {
    int i;
    cin >> i;
    uf.unite(edges[i - 1].a, edges[i - 1].b);
    sum += (long long)edges[i - 1].c;
  }

  vector<Edge> edges2;
  rep(i, m) {
    auto a = uf.find(edges[i].a), b = uf.find(edges[i].b);
    if (a != b) edges2.push_back(Edge{a, b, edges[i].c});
  }
  sort(edges2.begin(), edges2.end(),
       [&](const auto &l, const auto &r) { return l.c < r.c; });
  UnionFind uf2(n);
  for (auto e : edges2) {
    if (!uf.same(e.a, e.b)) {
      uf.unite(e.a, e.b);
      sum += (long long)e.c;
    }
  }

  auto tot = accumulate(
      edges.begin(), edges.end(), 0LL,
      [&](long long res, const auto &e) { return res + (long long)e.c; });

  cout << tot - sum << endl;

  return 0;
}
0