結果

問題 No.748 yuki国のお財布事情
ユーザー 0w10w1
提出日時 2018-10-19 21:58:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 5,964 ms
コンパイル使用メモリ 215,972 KB
実行使用メモリ 9,028 KB
最終ジャッジ日時 2023-08-12 00:54:00
合計ジャッジ時間 6,287 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 9 ms
4,380 KB
testcase_14 AC 18 ms
4,380 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 37 ms
4,888 KB
testcase_17 AC 62 ms
5,184 KB
testcase_18 AC 95 ms
7,012 KB
testcase_19 AC 121 ms
9,028 KB
testcase_20 AC 106 ms
8,552 KB
testcase_21 AC 84 ms
6,720 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 45 ms
4,664 KB
testcase_26 AC 68 ms
5,336 KB
testcase_27 AC 50 ms
4,884 KB
testcase_28 AC 79 ms
7,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

signed main() {
  ios::sync_with_stdio(false);

  int N, M, K;
  cin >> N >> M >> K;

  vector<tuple<int, int, int>> E(M);
  for (int i = 0; i < M; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    E[i] = make_tuple(w, u - 1, v - 1);
  }

  set<int> taken;
  for (int i = 0; i < K; ++i) {
    int e;
    cin >> e;
    taken.emplace(e - 1);
  }

  vector<int> fa(N);
  iota(fa.begin(), fa.end(), 0);
  function<int(int)> find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); };
  auto unite = [&](int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a == b) return false;
    fa[a] = b;
    return true;
  };

  int64_t ans = 0;
  for (int i = 0; i < M; ++i) ans += get<0>(E[i]);

  for (int i : taken) {
    int w, u, v;
    tie(w, u, v) = E[i];
    unite(u, v);
    ans -= w;
  }

  vector<int> eord(M);
  iota(eord.begin(), eord.end(), 0);
  sort(eord.begin(), eord.end(),
       [&](int i, int j) { return get<0>(E[i]) < get<0>(E[j]); });

  for (int i = 0; i < M; ++i) {
    if (taken.count(eord[i])) continue;
    int w, u, v;
    tie(w, u, v) = E[eord[i]];
    if (unite(u, v)) ans -= w;
  }

  cout << ans << endl;

  return 0;
}
0