티스토리 뷰

백준

백준 소스코드 [C++] 14950 정복자

Hani_Levenshtein 2020. 11. 27. 08:49

www.acmicpc.net/problem/14950

 

14950번: 정복자

서강 나라는 N개의 도시와 M개의 도로로 이루어졌다. 모든 도시의 쌍에는 그 도시를 연결하는 도로로 구성된 경로가 있다. 각 도로는 양방향 도로이며, 각 도로는 사용하는데 필요한 비용이 존재

www.acmicpc.net

백준 소스코드 [C++] 14950 정복자

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
typedef long long ll;
using namespace std;
int n, m,u,v,w,t;
vector<pii> adj[100000];
bool checked[100000];
priority_queue<pii,vector<pii>,greater<pii>> pq;
vector<int> res;
vector<int> prim() {
	checked[0] = true;
	for (auto there : adj[0])
		if (checked[there.second] == false)
			pq.push(there);
	while (pq.empty() != true) {
		int there = pq.top().second;
		int cost = pq.top().first;
		pq.pop();
		if (checked[there] == true) continue;
		checked[there] = true;
		res.push_back(cost);
		for (auto there : adj[there])
			if (checked[there.second] == false)
				pq.push(there);
	}

	return res;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m>>t;
	for (int i = 0;i < m;i++) {
		cin >> u >> v >> w;
		adj[u - 1].push_back({w,v-1 });
		adj[v - 1].push_back({w,u-1 });
	}
	vector<int> res = prim();
	sort(all(res));
	int ret = 0;
	for (int i = 0;i < (int)res.size();i++)ret += res[i]+t*i;
	cout << ret;
	return 0;
}
댓글