結果

問題 No.748 yuki国のお財布事情
ユーザー square1001
提出日時 2018-10-19 21:38:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,058 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 79,008 KB
実行使用メモリ 7,148 KB
最終ジャッジ日時 2024-11-18 20:13:20
合計ジャッジ時間 2,218 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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