結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
ldsyb
|
| 提出日時 | 2018-10-19 22:29:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 131 ms / 2,000 ms |
| コード長 | 1,281 bytes |
| コンパイル時間 | 1,754 ms |
| コンパイル使用メモリ | 176,788 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-18 21:28:02 |
| 合計ジャッジ時間 | 3,911 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct union_find
{
int n;
vector<int> t;
union_find(int n) : n(n)
{
t.assign(n, -1);
}
int size(int x)
{
return -t[root(x)];
}
int root(int x)
{
if (t[x] < 0)
{
return x;
}
return t[x] = root(t[x]);
}
bool same(int x, int y)
{
return root(x) == root(y);
}
bool merge(int x, int y)
{
if (same(x, y))
{
return false;
}
x = root(x);
y = root(y);
if (t[x] > t[y])
{
swap(x, y);
}
t[x] += t[y];
t[y] = x;
return true;
}
};
int main()
{
int64_t n, m, k, sum = 0;
cin >> n >> m >> k;
vector<pair<int64_t, pair<int, int>>> g(m);
for (int i = 0; i < m; i++)
{
int from, to;
int64_t cost;
cin >> from >> to >> cost;
from--;
to--;
sum += cost;
g[i] = {cost, {from, to}};
}
union_find uf(n);
int64_t mini = 0;
for (int i = 0; i < k; i++)
{
int c;
cin >> c;
c--;
int64_t cost = g[c].first;
int from = g[c].second.first, to = g[c].second.second;
mini += cost;
uf.merge(from, to);
}
sort(g.begin(), g.end());
for (auto &pp : g)
{
int64_t cost = pp.first;
int from = pp.second.first, to = pp.second.second;
if (uf.same(from, to))
{
continue;
}
uf.merge(from, to);
mini += cost;
}
cout << sum - mini << endl;
return 0;
}
ldsyb