結果

問題 No.748 yuki国のお財布事情
ユーザー azbrugbyazbrugby
提出日時 2019-02-28 22:20:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 356 ms / 2,000 ms
コード長 2,310 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 85,100 KB
実行使用メモリ 39,456 KB
最終ジャッジ日時 2023-09-05 16:21:39
合計ジャッジ時間 5,708 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,136 KB
testcase_01 AC 2 ms
6,064 KB
testcase_02 AC 3 ms
6,068 KB
testcase_03 AC 3 ms
6,148 KB
testcase_04 AC 3 ms
6,264 KB
testcase_05 AC 3 ms
6,148 KB
testcase_06 AC 2 ms
6,008 KB
testcase_07 AC 3 ms
6,152 KB
testcase_08 AC 3 ms
6,076 KB
testcase_09 AC 3 ms
6,328 KB
testcase_10 AC 3 ms
6,132 KB
testcase_11 AC 3 ms
6,132 KB
testcase_12 AC 3 ms
6,068 KB
testcase_13 AC 38 ms
10,192 KB
testcase_14 AC 85 ms
14,308 KB
testcase_15 AC 52 ms
14,148 KB
testcase_16 AC 147 ms
21,556 KB
testcase_17 AC 180 ms
20,872 KB
testcase_18 AC 284 ms
26,648 KB
testcase_19 AC 356 ms
39,456 KB
testcase_20 AC 320 ms
27,104 KB
testcase_21 AC 223 ms
20,288 KB
testcase_22 AC 2 ms
6,308 KB
testcase_23 AC 3 ms
6,060 KB
testcase_24 AC 2 ms
5,992 KB
testcase_25 AC 118 ms
19,612 KB
testcase_26 AC 175 ms
15,256 KB
testcase_27 AC 155 ms
14,252 KB
testcase_28 AC 251 ms
23,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <cstdio>
#include <cmath>
#define mk make_pair
#define pb push_back
#define printf printf_s
#define scanf scanf_s
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pos;
const ll MOD = 1000000007, N = 100001, dx[4] = { -1,1,0,0 }, dy[4] = { 0,0,-1,1 }, MIN = -72340172838, MAX = 72340172838;

ll mn(ll a, ll b) {
	if (a == -1)return b;
	if (b == -1)return a;
	return min(a, b);
}
ll gcd(ll v1, ll v2) {
	if (v1 == 0)return v2; if (v2 == 0)return v1; if (v2 > v1)return gcd(v2%v1, v1); return gcd(v1%v2, v2);
}

ll pw(ll v1, ll v2) {
	ll v3 = 1;
	while (v2 > 0) {
		if (v2 % 2)v3 = (v3*v1) % MOD;
		v1 = (v1*v1) % MOD;
		v2 /= 2;
	}
	return v3;
}

struct ab {
	ll a, b;
	bool operator<(const ab& right) const {
		return right.b*a - right.a*b < 0;
	}
};

ll n, m, k, ind[N], ans = 0;
vector<pos> g[N];
pair<pos, ll> edg[N];
priority_queue<pair<ll,pos> > q;

ll rt(ll ci) {
	if (ind[ci] == ci)return ci;
	return ind[ci] = rt(ind[ci]);
}

void mrg(ll i1, ll i2) {
	ind[rt(i1)] = rt(i2);
}

int main() {
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++)ind[i] = i;
	for (int i = 0; i < m;i++) {
		ll v1, v2, v3; 
		cin >> v1 >> v2 >> v3;
		edg[i] = mk(mk(v1, v2), v3);
		g[v1].pb(mk(v3, v2));
		g[v2].pb(mk(v3, v1));
		ans += v3;
	}
	while (k--) {
		ll v1; cin >> v1; v1--;
		ans -= edg[v1].second;
		mrg(edg[v1].first.first, edg[v1].first.second);
		ll i1 = edg[v1].first.first, i2 = edg[v1].first.second;
		for (int i = 0; i < g[i1].size(); i++) {
			q.push(mk(-g[i1][i].first, mk(g[i1][i].second, i1)));
		}
		for (int i = 0; i < g[i2].size(); i++) {
			q.push(mk(-g[i2][i].first, mk(g[i2][i].second, i2)));
		}
	}
	if (k == -1) {
		ll i2 = 1;
		for (int i = 0; i < g[i2].size(); i++) {
			q.push(mk(-g[i2][i].first, mk(g[i2][i].second, i2)));
		}
	}
	while (!q.empty()) {
		ll v1 = -q.top().first, ci = q.top().second.first, bi = q.top().second.second;
		q.pop();
		if (rt(ci) == rt(bi)) continue;
		mrg(ci, bi);
		ans -= v1;
		for (int i = 0; i < g[ci].size(); i++) {
			ll ni = g[ci][i].second,nd=g[ci][i].first;
			if (rt(ni) == rt(ci))continue;
			q.push(mk(-nd, mk(ni, ci)));
		}
	}
	cout << ans << endl;
	return 0;
}
0