当前位置: 代码迷 >> 综合 >> Bad Cowtractors POJ - 2377(kruskal)
  详细解决方案

Bad Cowtractors POJ - 2377(kruskal)

热度:31   发布时间:2023-12-07 00:26:27.0

Bad Cowtractors

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”.

Input

  • 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.
    Output

  • 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.

Sample Input

5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 1

Sample Output

42

Hint

OUTPUT DETAILS:

The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.

题解
求最大生成树,因为最后可能所有的点无法连接成树,可能为森林,所以用kruskal算法,如果最后有两个即以上树,则不成立,输出-1.

#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;struct edge{
    int to, from, cost;
};bool cmp (edge a,edge b) {
    return a.cost> b.cost;
}
edge d[20002];
int fa[1002];
int INF = 0x3f3f3f3f;int find(int x){
    if(fa[x] == x) return x;else return fa[x] = find(fa[x]);
}void unite(int x, int y) {
    fa[find(x)] = find (y);
}int main() {
    int N, M;scanf("%d %d", &N, &M) ;memset(d,0,sizeof(d));for (int i = 0; i < M; i++) {
    scanf("%d%d%d", &d[i].from ,&d[i].to ,&d[i].cost);}for (int i = 1; i <= N; i++) fa[i] = i;sort(d, d + M, cmp);long long ans = 0;int k = 0;/记录数的节点数,如果节点数为N个,(因为k初始值为0,所以k=N-1for (int i = 0; i < M; i++) {
    if (find(d[i].from) != find(d[i].to) ){
    unite(d[i].to, d[i].from);ans += d[i].cost;k++;}}if ( k == N - 1) printf("%lld\n", ans);else printf("-1\n");
}

优化高深版

#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct edge{
    int from, to, cost;edge() {
    } edge(int from, int to, int cost): from(from), to(to), cost(cost) {
    }bool operator < (const edge & other) const {
    return cost > other.cost;}
};int fa[1002];
int INF = 0x3f3f3f3f;struct Dsu {
    Dsu(int n): fa(n, -1) {
    }int find(int x) {
    return fa[x] < 0 ? x : fa[x] = find (fa[x]); }void unit(int u, int v) {
    u = find(u), v = find (v);if (u == v) return;if (fa[u] < fa[v]) swap(u, v);fa[v] += fa[u];fa[u] = v;}int size(int x) {
    return -fa[find(x)];}
private:
vector<int> fa;	
};int main() {
    int N, M;scanf("%d %d", &N, &M) ;vector<edge> d(M);for (int i = 0; i < M; i++) {
    scanf("%d%d%d", &d[i].from ,&d[i].to ,&d[i].cost); d[i].from --, d[i].to --;}sort(d.begin(), d.end());Dsu dsu(N);long long ans = 0;int k = 0;for (int i = 0; i < M; i++) {
    if (dsu.find(d[i].from) != dsu.find(d[i].to) ){
    dsu.unit(d[i].to, d[i].from);ans += d[i].cost;k++;}}if ( k == N - 1) printf("%lld\n", ans);else printf("-1\n");
}