結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
kyuna
|
| 提出日時 | 2019-07-20 14:08:45 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 146 ms / 2,000 ms |
| コード長 | 1,784 bytes |
| コンパイル時間 | 949 ms |
| コンパイル使用メモリ | 81,300 KB |
| 実行使用メモリ | 9,576 KB |
| 最終ジャッジ日時 | 2024-12-29 17:28:25 |
| 合計ジャッジ時間 | 3,380 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> data;
int __size;
UnionFind(int size) : data(size, -1), __size(size) { }
bool unionSet(int x, int y) {
if ((x = root(x)) != (y = root(y))) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x; __size--;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
int size() { return __size; }
};
template <typename T>
struct Edge { int src, dst; T cost;
Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }
Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }
};
template <typename T> using Edges = vector<Edge<T>>;
template<typename T>
pair<T, Edges<T>> kruskal(Edges<T> &edges, Edges<T> &fixed, int v) {
sort(begin(edges), end(edges), [](const Edge<T> &a, const Edge<T> &b) {
return a.cost < b.cost;
});
UnionFind uf(v);
T total = 0; Edges<T> F;
for (auto &e: fixed) {
uf.unionSet(e.src, e.dst), total += e.cost, F.emplace_back(e);
}
for (auto &e: edges) {
if (uf.unionSet(e.src, e.dst)) total += e.cost, F.emplace_back(e);
}
return {total, F};
}
int main() {
int V, E, K; cin >> V >> E >> K;
Edges<long long> edges, fixed;
long long sum = 0;
while (E--) {
int s, t, w; cin >> s >> t >> w; s--, t--;
edges.emplace_back(s, t, w);
sum += w;
}
while (K--) {
int e; cin >> e; e--;
fixed.emplace_back(edges[e]);
}
cout << sum - kruskal(edges, fixed, V).first << endl;
return 0;
}
kyuna