結果

問題 No.748 yuki国のお財布事情
ユーザー poyompypoyompy
提出日時 2018-10-19 21:34:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,028 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 73,076 KB
実行使用メモリ 4,992 KB
最終ジャッジ日時 2023-08-12 00:17:42
合計ジャッジ時間 3,500 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 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 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 25 ms
4,380 KB
testcase_15 AC 17 ms
4,380 KB
testcase_16 AC 46 ms
4,376 KB
testcase_17 AC 93 ms
4,576 KB
testcase_18 AC 110 ms
4,992 KB
testcase_19 AC 122 ms
4,884 KB
testcase_20 AC 108 ms
4,616 KB
testcase_21 AC 104 ms
4,836 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 86 ms
4,572 KB
testcase_26 AC 103 ms
4,932 KB
testcase_27 AC 101 ms
4,828 KB
testcase_28 AC 83 ms
4,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct DSU {
  vector<int> dat;
  
  DSU(int n) : dat(n, -1) {}

  int find(int u) {
    if (dat[u] < 0) return u;
    return dat[u] = find(dat[u]);
  }

  bool unite(int u, int v) {
    u = find(u);
    v = find(v);
    if (u == v) return false;
    if (dat[u] > dat[v]) swap(u, v);
    dat[u] += dat[v];
    dat[v] = u;
    return true;
  }
};


int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<pair<int, int>> es(m);
  vector<int> us(m), vs(m);
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    int c;
    cin >> us[i] >> vs[i] >> c;
    es[i] = make_pair(c, i);
  }
  DSU dsu(n + 1);
  vector<bool> can(m, true);
  for (int i = 0; i < k; i++) {
    int e;
    cin >> e;
    e--;
    can[e] = false;
    dsu.unite(us[e], vs[e]);
  }
  sort(es.begin(), es.end());
  for (auto e : es) {
    if (!can[e.second]) continue;
    if (!dsu.unite(us[e.second], vs[e.second])) {
      ans += e.first;
    }
  }
  cout << ans << endl;
}
0