結果

問題 No.748 yuki国のお財布事情
ユーザー ikdikd
提出日時 2018-10-19 06:53:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 1,882 bytes
コンパイル時間 1,297 ms
コンパイル使用メモリ 88,176 KB
実行使用メモリ 11,728 KB
最終ジャッジ日時 2024-04-26 16:32:31
合計ジャッジ時間 4,705 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 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 2 ms
5,376 KB
testcase_06 AC 2 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 16 ms
5,376 KB
testcase_14 AC 27 ms
5,376 KB
testcase_15 AC 18 ms
5,376 KB
testcase_16 AC 50 ms
5,376 KB
testcase_17 AC 119 ms
8,908 KB
testcase_18 AC 128 ms
8,140 KB
testcase_19 AC 136 ms
8,396 KB
testcase_20 AC 121 ms
7,632 KB
testcase_21 AC 135 ms
10,060 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 102 ms
8,216 KB
testcase_26 AC 142 ms
11,212 KB
testcase_27 AC 150 ms
11,728 KB
testcase_28 AC 91 ms
6,092 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]++;
    }
  }
};

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;
  }
  struct E {
    int to, c;
    bool operator<(const E &r) const { return c > r.c; }
  };
  vector<vector<E>> g(n);
  rep(i, m) {
    auto a = uf.find(edges[i].a), b = uf.find(edges[i].b);
    if (a != b) {
      g[a].push_back(E{b, edges[i].c});
      g[b].push_back(E{a, edges[i].c});
    }
  }

  vector<int> visited(n, false);
  priority_queue<E> q;
  auto s = uf.find(0);
  visited[s] = true;
  for (auto e : g[s]) {
    q.push(e);
  }
  while (q.size() > 0) {
    auto e = q.top();
    q.pop();
    if (visited[e.to]) continue;
    visited[e.to] = true;
    sum += (long long)e.c;
    for (auto ne : g[e.to]) {
      if (!visited[ne.to]) q.push(ne);
    }
  }

  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