티스토리 뷰

백준

백준 소스코드 [C++] 1647 도시 분할 계획

Hani_Levenshtein 2020. 11. 26. 16:06

www.acmicpc.net/problem/1647

 

1647번: 도시 분할 계획

첫째 줄에 집의 개수N, 길의 개수M이 주어진다. N은 2이상 100,000이하인 정수이고, M은 1이상 1,000,000이하인 정수이다. 그 다음 줄부터 M줄에 걸쳐 길의 정보가 A B C 세 개의 정수로 주어지는데 A번 집

www.acmicpc.net

백준 소스코드 [C++] 1647 도시 분할 계획

#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;
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;
	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() - 1;i++)ret += res[i];
	cout << ret;
	return 0;
}
댓글