結果
問題 | No.748 yuki国のお財布事情 |
ユーザー |
|
提出日時 | 2018-10-20 01:27:47 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 51 ms / 2,000 ms |
コード長 | 1,854 bytes |
コンパイル時間 | 2,029 ms |
コンパイル使用メモリ | 176,416 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-19 00:35:11 |
合計ジャッジ時間 | 3,779 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;struct UnionFind {//各要素が属する集合の代表(根)を管理する//もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しいvector<int> data;UnionFind(int sz) : data(sz, -1) {}bool unite(int x, int y) {x = find(x);y = find(y);bool is_union = (x != y);if (is_union) {if (data[x] > data[y]) swap(x, y);data[x] += data[y];data[y] = x;}return is_union;}int find(int x) {if (data[x] < 0) { //要素xが根であるreturn x;} else {data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新されるreturn data[x];}}bool same(int x, int y) {return find(x) == find(y);}int size(int x) {return -data[find(x)];}};struct Edge {int u, v;ll cost;bool operator<(const Edge& e) const {return cost < e.cost;}};int main() {cin.tie(0);ios::sync_with_stdio(false);int n, m, k;cin >> n >> m >> k;vector<Edge> es;ll ans = 0;for (int i = 0; i < m; i++) {int a, b;ll c;cin >> a >> b >> c;a--; b--;es.push_back({a, b, c});ans += c;}UnionFind uf(n);for (int i = 0; i < k; i++) {int e;cin >> e;e--;uf.unite(es[e].u, es[e].v);ans -= es[e].cost;}sort(es.begin(), es.end());for (Edge& e : es) {if (!uf.same(e.u, e.v)) {uf.unite(e.u, e.v);ans -= e.cost;}}cout << ans << endl;return 0;}