λ¬Έμ
μ½λ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M, t; //N=λμμ κ°μ(λ
Έλ), M=λλ‘μ κ°μ(κ°μ ), t=μ 볡ν λλ§λ€ λλ λμ λΉμ©
vector<pair<int, pair<int, int>>>edges;
int parent[10001];
int ans = 0, cnt = 0;;
int findParent(int x) {
if (x == parent[x]) return x;
else return x = findParent(parent[x]);
}
void unionParent(int a, int b) {
a = findParent(a);
b = findParent(b);
if (a < b) parent[b] = a;
else parent[a] = b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> t;
for (int i = 1; i <= N; i++) { //λΆλͺ¨ ν
μ΄λΈμμ, λΆλͺ¨μμ μ μκΈ°μμ μΌλ‘ μ΄κΈ°ν
parent[i] = i;
}
for (int i = 0; i < M; i++) {
int a, b, c;
cin >> a >> b >> c;
edges.push_back({ c,{a,b} }); //λΉμ©μ κΈ°μ€μΌλ‘ μ λ ¬νκΈ° μν΄ ννμ 첫λ²μ§Έ μμμ λΉμ© μ½μ
}
sort(edges.begin(), edges.end());
for (int i = 0; i < edges.size(); i++) {
int cost = edges[i].first;
int a = edges[i].second.first;
int b = edges[i].second.second;
if (findParent(a) != findParent(b)) {
unionParent(a, b);
ans += cost+cnt*t;
cnt++;
}
}
cout << ans;
}
'Algorithm π§π»βπ» > λ°±μ€(BOJ)' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€,c++] 1541λ² - μμ΄λ²λ¦° κ΄νΈ (0) | 2021.11.17 |
---|---|
[λ°±μ€,c++] 15240λ² - Paint bucket (0) | 2021.11.17 |
[λ°±μ€,c++] 1504λ² - νΉμ ν μ΅λ¨ κ²½λ‘ (0) | 2021.11.17 |
[λ°±μ€,c++] 14938λ² - μκ°κ·ΈλΌμ΄λ (0) | 2021.11.14 |
[λ°±μ€,c++] 1439λ² - λ€μ§κΈ° (0) | 2021.11.14 |
[λ°±μ€,c++] 14923λ² - λ―Έλ‘ νμΆ (0) | 2021.11.14 |
λκΈ