結果

問題 No.748 yuki国のお財布事情
ユーザー square1001square1001
提出日時 2018-10-19 21:38:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,058 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 80,356 KB
実行使用メモリ 7,016 KB
最終ジャッジ日時 2024-04-29 15:31:35
合計ジャッジ時間 2,033 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 8 ms
5,376 KB
testcase_16 AC 17 ms
5,376 KB
testcase_17 AC 37 ms
6,632 KB
testcase_18 AC 41 ms
5,996 KB
testcase_19 AC 41 ms
5,376 KB
testcase_20 AC 36 ms
5,376 KB
testcase_21 AC 38 ms
6,252 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 35 ms
6,636 KB
testcase_26 AC 41 ms
7,016 KB
testcase_27 AC 41 ms
7,016 KB
testcase_28 AC 29 ms
5,376 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