結果

問題 No.748 yuki国のお財布事情
ユーザー square1001square1001
提出日時 2018-10-19 21:38:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,058 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 79,260 KB
実行使用メモリ 7,052 KB
最終ジャッジ日時 2023-08-12 00:23:09
合計ジャッジ時間 2,689 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,388 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 18 ms
4,384 KB
testcase_17 AC 40 ms
6,364 KB
testcase_18 AC 42 ms
5,908 KB
testcase_19 AC 43 ms
4,800 KB
testcase_20 AC 39 ms
4,732 KB
testcase_21 AC 41 ms
5,896 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 38 ms
6,384 KB
testcase_26 AC 44 ms
7,052 KB
testcase_27 AC 44 ms
6,936 KB
testcase_28 AC 30 ms
4,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct edge {
	int a, b, cost;
};
int par[100009];
int root(int x) {
	if (x == par[x]) return x;
	return par[x] = root(par[x]);
}
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, M, K;
	cin >> N >> M >> K;
	vector<edge> g;
	long long sumlength = 0;
	for (int i = 0; i < M; ++i) {
		int a, b, c;
		cin >> a >> b >> c; --a, --b;
		sumlength += c;
		g.push_back(edge{ a, b, c });
	}
	vector<bool> use(M);
	for (int i = 0; i < K; ++i) {
		int x;
		cin >> x; --x;
		use[x] = true;
	}
	for (int i = 0; i < N; ++i) par[i] = i;
	vector<edge> edgelist;
	for (int i = 0; i < M; ++i) {
		if (use[i]) {
			par[root(g[i].a)] = root(g[i].b);
			sumlength -= g[i].cost;
		}
		else {
			edgelist.push_back(g[i]);
		}
	}
	sort(edgelist.begin(), edgelist.end(), [](edge e1, edge e2) { return e1.cost < e2.cost; });
	for (edge e : edgelist) {
		if (root(e.a) != root(e.b)) {
			par[root(e.a)] = root(e.b);
			sumlength -= e.cost;
		}
	}
	cout << sumlength << '\n';
	return 0;
}
0