結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2018-10-19 21:49:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 54 ms / 2,000 ms |
| コード長 | 1,990 bytes |
| コンパイル時間 | 1,796 ms |
| コンパイル使用メモリ | 175,264 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-18 20:24:56 |
| 合計ジャッジ時間 | 3,335 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }
struct QU {
V<> par, rank, _size;
QU(int n) : par(n), rank(n), _size(n, 1) {
iota(par.begin(), par.end(), 0);
}
int find(int a) {
if (par[a] == a) return a;
return par[a] = find(par[a]);
}
bool same(int a, int b) { return find(a) == find(b); }
int size(int a) { return _size[find(a)]; }
void unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (rank[a] < rank[b]) {
par[a] = b;
_size[b] += _size[a];
} else {
par[b] = a;
_size[a] += _size[b];
}
if (rank[a] == rank[b]) rank[a]++;
}
};
template<class T> struct edge { int from, to; T w; edge(int a, int b, T w) : from(a), to(b), w(w) {} };
template<class T> T kruskal(V< edge<T> >& g, int n) {
T res = 0;
QU qu(n);
sort(g.begin(), g.end(), [](const auto& a, const auto& b) { return a.w < b.w; });
for (auto&& e : g) {
if (!qu.same(e.from, e.to)) {
qu.unite(e.from, e.to);
res += e.w;
}
}
return res;
}
int main() {
cin.tie(NULL); ios::sync_with_stdio(false);
int n, m, k; cin >> n >> m >> k;
V< edge<lint> > g;
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c, a--, b--;
g.emplace_back(a, b, c);
}
QU qu(n);
lint res = 0;
for (int i = 0; i < k; i++) {
int e; cin >> e, e--;
res += g[e].w;
qu.unite(g[e].from, g[e].to);
}
sort(g.begin(), g.end(), [](const auto& a, const auto& b) { return a.w < b.w; });
lint s = 0;
for (auto&& e : g) {
s += e.w;
if (!qu.same(e.from, e.to)) {
res += e.w;
qu.unite(e.from, e.to);
}
}
cout << s - res << '\n';
}
risujiroh