結果

問題 No.748 yuki国のお財布事情
ユーザー atudnatudn
提出日時 2022-11-27 22:01:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 2,836 bytes
コンパイル時間 2,105 ms
コンパイル使用メモリ 209,008 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 03:23:58
合計ジャッジ時間 5,134 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 15 ms
6,944 KB
testcase_14 AC 26 ms
6,940 KB
testcase_15 AC 18 ms
6,944 KB
testcase_16 AC 48 ms
6,944 KB
testcase_17 AC 100 ms
6,940 KB
testcase_18 AC 112 ms
6,944 KB
testcase_19 AC 119 ms
6,944 KB
testcase_20 AC 106 ms
6,944 KB
testcase_21 AC 110 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 94 ms
6,940 KB
testcase_26 AC 114 ms
6,940 KB
testcase_27 AC 110 ms
6,940 KB
testcase_28 AC 80 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define SELECTER(_1,_2,_3,SELECT,...) SELECT
#define rep1(i,n) for(int i=0;i<(int)n;++i)
#define rep2(i,a,n) for(int i=(int)a;i<(int)n;++i)
#define rep(...) SELECTER(__VA_ARGS__,rep2,rep1) (__VA_ARGS__)
#define fi first
#define se second
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
using veci=vector<int>;
using vecpii=vector<pair<int,int>>;
using vecll=vector<long long>;
using vecpll=vector<pair<long long,long long>>;
using vecs=vector<string>;
using vecb=vector<bool>;
using vecd=vector<double>;
template<class T> inline bool chmax(T& a,T b){if(a<b) {a=b;return true;} return false;}
template<class T> inline bool chmin(T& a,T b){if(a>b) {a=b;return true;} return false;}
 
constexpr double PI=3.14159265359;
constexpr int iMAX=2147483647;
constexpr ll lMAX=9223372036854775807;
//constexpr int INF=1<<30;
constexpr ll INF=1LL<<60;
//constexpr int MOD=1000000007;
//constexpr ll MOD=1000000007;
constexpr ll MOD=998244353;
//constexpr int LOG=62;

struct UnionFind{
	veci par_size;
	int _n;

	UnionFind(int n):_n(n),par_size(n,-1){}

	int root(int a){
		assert(0 <= a && a < _n);
		if(par_size[a]<0) return a;
		return par_size[a]=root(par_size[a]);
	}

	void unite(int a,int b){
		assert(0 <= a && a < _n);
    assert(0 <= b && b < _n);
		int ra=root(a);int rb=root(b);
		if(ra==rb) return;
		if(-par_size[ra]<-par_size[rb]) swap(ra,rb);
		par_size[ra]+=par_size[rb];
		par_size[rb]=ra;
	}

	bool same(int a,int b){
		assert(0 <= a && a < _n);
    assert(0 <= b && b < _n);
		return root(a)==root(b);
	}

	int size(int a) {
    assert(0 <= a && a < _n);
    return -par_size[root(a)];
  }

	vector<veci> groups() {
    veci leader_buf(_n), group_size(_n);
    for (int i = 0; i < _n; i++) {
    	leader_buf[i] = root(i);
      group_size[leader_buf[i]]++;
    }
    vector<veci> result(_n);
    for (int i = 0; i < _n; i++) {
      result[i].reserve(group_size[i]);
    }
    for (int i = 0; i < _n; i++) {
      result[leader_buf[i]].push_back(i);
    }
    result.erase(
      remove_if(result.begin(), result.end(),
              [&](const veci& v) {return v.empty();}),
      result.end());
    return result;
  }
};

int main(){
	//ifstream in("input.txt");
	//cin.rdbuf(in.rdbuf());
	int n,m,k;cin>>n>>m>>k;
	veci a(m),b(m),c(m);
	ll sum=0;
	rep(i,m) {
		cin>>a[i]>>b[i]>>c[i];
		a[i]--;b[i]--;
	}
	UnionFind uni(n);
	vecb used(m,false);
	rep(i,k){
		int v;cin>>v;
		v--;
		uni.unite(a[v],b[v]);
		used[v]=true;
	}

	priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>> que;
	rep(i,m){
		if(!used[i]) que.emplace(c[i],pii{a[i],b[i]});
	}
	ll ans=0;
	while(que.size()){
		int cv=que.top().fi;
		pii v=que.top().se;
		que.pop();
		if(uni.same(v.fi,v.se)) {ans+=cv;continue;}
		uni.unite(v.fi,v.se);
	}
	cout<<ans<<endl;
}
0