結果

問題 No.748 yuki国のお財布事情
ユーザー FF256grhyFF256grhy
提出日時 2018-10-19 21:59:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 2,614 bytes
コンパイル時間 2,023 ms
コンパイル使用メモリ 176,616 KB
実行使用メモリ 16,880 KB
最終ジャッジ日時 2023-08-12 00:55:40
合計ジャッジ時間 4,657 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,512 KB
testcase_01 AC 2 ms
5,384 KB
testcase_02 AC 2 ms
5,600 KB
testcase_03 AC 2 ms
5,488 KB
testcase_04 AC 2 ms
5,464 KB
testcase_05 AC 2 ms
5,512 KB
testcase_06 AC 2 ms
5,676 KB
testcase_07 AC 2 ms
5,448 KB
testcase_08 AC 2 ms
5,380 KB
testcase_09 AC 2 ms
5,448 KB
testcase_10 AC 2 ms
5,384 KB
testcase_11 AC 2 ms
5,432 KB
testcase_12 AC 2 ms
5,400 KB
testcase_13 AC 16 ms
5,976 KB
testcase_14 AC 28 ms
6,392 KB
testcase_15 AC 19 ms
6,012 KB
testcase_16 AC 54 ms
7,876 KB
testcase_17 AC 108 ms
10,136 KB
testcase_18 AC 133 ms
12,564 KB
testcase_19 AC 163 ms
15,344 KB
testcase_20 AC 140 ms
14,588 KB
testcase_21 AC 136 ms
15,000 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
5,388 KB
testcase_24 AC 2 ms
5,448 KB
testcase_25 AC 96 ms
8,824 KB
testcase_26 AC 137 ms
16,880 KB
testcase_27 AC 135 ms
15,548 KB
testcase_28 AC 105 ms
11,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

struct UnionFind {
	int s, num; // [0, s) の s 個の要素がある。num は非空集合の個数。
	vector<vector<int>> v;
	vector<int> find;
	
	UnionFind() { s = 0; }
	UnionFind(int s) { init(s); }
	void init(int arg_s) {
		num = s = arg_s;
		v.clear();
		find.clear();
		inc(i, s) { v.PB({i}); find.PB(i); }
	}
	bool unite(int x, int y) {
		assert(inID(x, 0, s) && inID(y, 0, s));
		if(find[x] == find[y]) { return false; }
		num--;
		if(v[find[x]].size() > v[find[y]].size()) { swap(x, y); }
		int fx = find[x];
		for(auto && e: v[fx]) { v[find[y]].PB(e); find[e] = find[y]; }
		v[fx].clear();
		return true;
	}
};

// ----

const int M = 100000;

LL n, m, k, a[M], b[M], c[M], e[M];
array<LL, 3> p[M];

int main() {
	cin >> n >> m >> k;
	inc(i, m) { cin >> a[i] >> b[i] >> c[i]; a[i]--; b[i]--; }
	inc(i, k) { cin >> e[i]; e[i]--; }
	
	inc(i, m) { p[i] = { 1, c[i], i }; }
	inc(i, k) { p[e[i]][0] = 0; }
	sort(p, p + m);
	
	LL s = 0;
	UnionFind uf(n);
	inc(i, m) {
		if(! uf.unite(a[p[i][2]], b[p[i][2]]) && p[i][0] == 1) { s += p[i][1]; }
	}
	
	cout << s << endl;
	
	return 0;
}
0