結果

問題 No.748 yuki国のお財布事情
ユーザー ldsybldsyb
提出日時 2018-10-19 22:29:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 1,281 bytes
コンパイル時間 2,019 ms
コンパイル使用メモリ 174,720 KB
実行使用メモリ 5,012 KB
最終ジャッジ日時 2023-08-12 01:28:29
合計ジャッジ時間 4,674 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 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 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 14 ms
4,376 KB
testcase_14 AC 26 ms
4,384 KB
testcase_15 AC 17 ms
4,380 KB
testcase_16 AC 47 ms
4,380 KB
testcase_17 AC 94 ms
4,696 KB
testcase_18 AC 111 ms
4,908 KB
testcase_19 AC 124 ms
4,900 KB
testcase_20 AC 111 ms
4,644 KB
testcase_21 AC 105 ms
4,956 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 87 ms
4,692 KB
testcase_26 AC 103 ms
5,012 KB
testcase_27 AC 103 ms
4,960 KB
testcase_28 AC 84 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0