バックアップアルゴリズム7044号:Bad Contractors
16341 ワード
リンク
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を取得すればよい.Reference
この問題について(バックアップアルゴリズム7044号:Bad Contractors), 我々は、より多くの情報をここで見つけました https://velog.io/@inwooleeme/백준-알고리즘-7044번-Bad-Cowtractorsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol