結果

問題 No.748 yuki国のお財布事情
ユーザー startcpp
提出日時 2018-10-19 22:55:04
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,015 bytes
コンパイル時間 872 ms
コンパイル使用メモリ 71,432 KB
実行使用メモリ 9,640 KB
最終ジャッジ日時 2024-11-18 22:07:40
合計ジャッジ時間 3,062 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
typedef tuple<int, int, int> T;

struct UF {
	int par[100000];
	UF() { int i; rep(i, 100000) par[i] = i; }
	int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); }
	void unit(int x, int y) { x = root(x); y = root(y); par[x] = y; }
	bool same(int x, int y) { return root(x) == root(y); }
};

int n, m, k;
int a[100000], b[100000], c[100000];
int e[100000];
T edges[100000];
UF uf;

signed main() {
	int i;
	
	cin >> n >> m >> k;
	rep(i, m) { cin >> a[i] >> b[i] >> c[i]; a[i]--; b[i]--; }
	rep(i, k) { cin >> e[i]; e[i]--; c[e[i]] = 0; }
	rep(i, m) { edges[i] = T(c[i], a[i], b[i]); }
	sort(edges, edges + m);
	
	int ans = 0;
	rep(i, m) {
		int cst = get<0>(edges[i]);
		int u = get<1>(edges[i]);
		int v = get<2>(edges[i]);
		if (uf.same(u, v)) {
			ans += cst;
		}
		else {
			uf.unit(u, v);
		}
	}
	cout << ans << endl;
	return 0;
}
0