백준 알고리즘 7044번 : Bad Cowtractors
링크
https://www.acmicpc.net/problem/7044
문제
Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
입력
Line 1: Two space-separated integers: N and M
Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
출력
Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
예제 입력 및 출력
풀이 코드(C++)
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define sz(a) int((a).size())
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using wector = vector<vector<int>>;
using tii = tuple<int,int,int>;
struct UnionFind {
vector<int> parent, rank, cnt;
UnionFind(int n) : parent(n), rank(n, 1), cnt(n, 1) {
iota(parent.begin(), parent.end(), 0);
}
int Find(int x) {
return x == parent[x] ? x : parent[x] = Find(parent[x]);
}
bool Union(int a, int b) {
a = Find(a), b = Find(b);
if (a == b) return 0;
if (rank[a] < rank[b]) swap(a, b);
parent[b] = a;
rank[a] += rank[a] == rank[b];
cnt[a] += cnt[b];
return 1;
}
};
int main() {
fastio;
int n,m; cin >> n >> m;
vector<tii> e;
while(m--){
int a,b,c; cin >> a >> b >> c;
e.pb({c,a,b});
e.pb({c,b,a});
}
sort(all(e));
reverse(all(e));
int cnt = 0;
ll cost = 0;
UnionFind UF(n + 1);
for(auto [c,a,b] : e){
a = UF.Find(a),b = UF.Find(b);
if(a == b) continue;
UF.Union(a, b);
cost += c;
if(++cnt == n - 1) break;
}
cout << (cnt != n- 1 ? -1 : cost) << "\n";
return 0;
}
가중치가 큰 순서대로 MST를 구성하라고 하였기 때문에 Edge를 정렬한 후에 뒤집어서 MST를 구해주면 됩니다.
Author And Source
이 문제에 관하여(백준 알고리즘 7044번 : Bad Cowtractors), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@inwooleeme/백준-알고리즘-7044번-Bad-Cowtractors저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)