結果

問題 No.748 yuki国のお財布事情
ユーザー chocoruskchocorusk
提出日時 2018-10-19 21:48:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,404 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 101,084 KB
実行使用メモリ 7,888 KB
最終ジャッジ日時 2024-04-29 15:40:09
合計ジャッジ時間 2,890 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 27 ms
5,376 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 50 ms
5,448 KB
testcase_17 AC 98 ms
7,304 KB
testcase_18 AC 104 ms
6,440 KB
testcase_19 AC 111 ms
5,912 KB
testcase_20 AC 102 ms
5,888 KB
testcase_21 AC 103 ms
6,804 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 87 ms
6,988 KB
testcase_26 AC 104 ms
7,888 KB
testcase_27 AC 103 ms
7,496 KB
testcase_28 AC 78 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, P> Pll;
int par[100002];
int rk[100002];
void init(int n){
	for(int i=0; i<n; i++){
		par[i]=i;
	}
}
int find(int x){
	if(par[x]==x){
		return x;
	}else{
		return par[x]=find(par[x]);
	}
}
void unite(int x, int y){
	x=find(x);
	y=find(y);
	if(x==y) return;
	if(rk[x]<rk[y]){
		par[x]=y;
	}else{
		par[y]=x;
		if(rk[x]==rk[y]) rk[x]++;
	}
}
bool same(int x, int y){
	return find(x)==find(y);
}
int main()
{
	int n, m, k;
	cin>>n>>m>>k;
	init(n);
	ll c[100000];
	int a[100000], b[100000];
	ll tot=0;
	for(int i=0; i<m; i++){
		cin>>a[i]>>b[i]>>c[i];
		a[i]--; b[i]--;
		tot+=c[i];
	}
	bool used[100000]={};
	ll sum=0;
	for(int i=0; i<k; i++){
		int e;
		cin>>e;
		e--;
		used[e]=1;
		sum+=c[e];
		unite(a[e], b[e]);
	}
	vector<Pll> ed;
	for(int i=0; i<m; i++){
		if(!used[i]) ed.push_back(Pll(c[i], P(a[i], b[i])));
	}
	sort(ed.begin(), ed.end());
	for(int i=0; i<ed.size(); i++){
		int a1=ed[i].second.first, b1=ed[i].second.second;
		if(!same(a1, b1)){
			unite(a1, b1);
			sum+=ed[i].first;
		}
	}
	cout<<tot-sum<<endl;
	return 0;
}
0