結果

問題 No.748 yuki国のお財布事情
ユーザー furafura
提出日時 2020-06-08 09:30:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 2,029 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 222,448 KB
実行使用メモリ 28,164 KB
最終ジャッジ日時 2023-08-26 01:46:54
合計ジャッジ時間 5,437 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 15 ms
5,908 KB
testcase_15 AC 9 ms
4,852 KB
testcase_16 AC 31 ms
9,252 KB
testcase_17 AC 60 ms
14,184 KB
testcase_18 AC 88 ms
21,132 KB
testcase_19 AC 98 ms
27,248 KB
testcase_20 AC 84 ms
25,416 KB
testcase_21 AC 91 ms
26,500 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 41 ms
10,452 KB
testcase_26 AC 94 ms
28,164 KB
testcase_27 AC 92 ms
27,692 KB
testcase_28 AC 59 ms
18,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

class union_find{
	int n;
	vector<int> p;
public:
	union_find(int n):n(n),p(n,-1){}
	int find(int u){ return p[u]<0?u:p[u]=find(p[u]); }
	void unite(int u,int v){
		u=find(u); v=find(v);
		if(u!=v){
			if(p[v]<p[u]) swap(u,v);
			p[u]+=p[v]; p[v]=u; n--;
		}
	}
	bool is_same(int u,int v){ return find(u)==find(v); }
	int size()const{ return n; }
	int size(int u){ return -p[find(u)]; }
};

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

template<class T>
void add_undirected_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
	G[v].emplace_back(u,wt);
}

template<class T>
void add_directed_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
}

template<class T>
pair<T,weighted_graph<T>> Kruskal(const weighted_graph<T>& G){
	int n=G.size();
	vector<tuple<T,int,int>> E;
	rep(u,n) for(const auto& e:G[u]) {
		int v=e.to;
		if(u<v) E.emplace_back(e.wt,u,v);
	}

	sort(E.begin(),E.end());

	T ans{};
	weighted_graph<T> Res(n);
	union_find U(n);
	for(const auto& e:E){
		if(U.size()==1) break;
		T wt;
		int u,v; tie(wt,u,v)=e;
		if(!U.is_same(u,v)){
			U.unite(u,v);
			ans+=wt;
			Res[u].emplace_back(v,wt);
			Res[v].emplace_back(u,wt);
		}
	}
	return {ans,Res};
}

int main(){
	int n,m,k; scanf("%d%d%d",&n,&m,&k);
	vector<tuple<int,int,int>> E(m);
	rep(i,m){
		scanf("%d%d%d",&get<0>(E[i]),&get<1>(E[i]),&get<2>(E[i]));
		get<0>(E[i])--;
		get<1>(E[i])--;
	}
	vector<int> e(k);
	rep(i,k) scanf("%d",&e[i]), e[i]--;

	lint total=0;
	int idx=0;
	weighted_graph<lint> G(n);
	rep(i,m){
		if(idx<k && i==e[idx]){
			add_undirected_edge<lint>(G,get<0>(E[i]),get<1>(E[i]),0);
			idx++;
		}
		else{
			add_undirected_edge<lint>(G,get<0>(E[i]),get<1>(E[i]),get<2>(E[i]));
			total+=get<2>(E[i]);
		}
	}
	printf("%lld\n",total-Kruskal(G).first);

	return 0;
}
0