結果

問題 No.2200 Weird Shortest Path
ユーザー warabi0906
提出日時 2023-02-08 19:44:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 3,302 ms
コンパイル使用メモリ 236,532 KB
実行使用メモリ 8,404 KB
最終ジャッジ日時 2024-07-06 05:50:34
合計ジャッジ時間 9,264 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "atcoder/all"
#define debug cout << "OK" << endl;
template<typename T>
inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }
inline int Code(char c) {
	if ('A' <= c and c <= 'Z')return (int)(c - 'A');
	if ('a' <= c and c <= 'z')return (int)(c - 'a');
	if ('0' <= c and c <= '9')return (int)(c - '0');
	assert(false);
}
using namespace std;
using namespace atcoder;
#define int long long
constexpr int MOD = 998244353;
constexpr int INF = (1LL << 63);
using minit = modint998244353;

template<typename T>
ostream& operator << (ostream& st, const vector<T>& v) {
	for (const T value : v) {
		st << value << " ";
	}
	return st;
}
template<typename T>
istream& operator >> (istream& st, vector<T>& v) {
	for (T& value : v) {
		st >> value;
	}
	return st;
}

//

//

void solve() {
	int N, M;
	cin >> N >> M;
	vector<pair<int, pair<int, int>>> E(M);
	for (int e = 0; e < M; e++) {
		int u, v, w;
		cin >> u >> v >> w;
		u--; v--;
		E[e] = { w,{u,v} };
	}
	sort(E.begin(), E.end());
	dsu d(N);
	int ans = 0;
	for (int i = 0; i < M; i++) {
		if (d.same(E[i].second.first, E[i].second.second))continue;
		ans += (int)d.size(E[i].second.first) * (int)d.size(E[i].second.second) * E[i].first;
		d.merge(E[i].second.first, E[i].second.second);
	}
	cout << ans << endl;
}
signed main() {
	int T = 1;
	// cin >> T;
	for (int i = 0; i < T; i++)
		solve();
	return 0;
}

/*
* わらびのコードこーどすぺーす
 (σ・∀・)σゲッツ!!
*/
0