1. 程式人生 > >(poj 2377)Kruskal演算法 最大生成樹

(poj 2377)Kruskal演算法 最大生成樹

這道題只是一道模板題,感到唯一的坑點就是n,m容易打錯,一定要注意結構體要開到Max(M)+n; 之前便是因為這個地方Runtime Error了兩次;順便注意最後輸出的答案 為long long型

Kruskal演算法通過把所有的邊從小到大排列後,不斷取權值最小的邊加入最小生成樹(起初可能是離散的多個樹,最終連成一個整體),並通過並查集來捨棄形成迴路的邊。

題目

**Description
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 17
    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.**
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100+20000;
struct Node
{
    int a;
    int b;
    long long price;
}s[N];
int ran[N];
int uset[N];
bool cmp(struct Node a,struct Node b)  //按權值由大到小排序
{
    return a.price>b.price;
}
void makeset(int n)   //並查集初始化
{
    for(int i=1;i<=n;i++)
        uset[i]=i,ran[i]=0;
}
int find(int x)   // 迴圈實現路基壓縮
{
    int r=x,t;
    while(r!=uset[r])
       r=uset[r];
    while(x!=r)
    {
        t=uset[x];
        uset[x]=r;
        x=t;
    }
    return x;
}
/*int find(int x)
{
    if(x!=uset[x])
        uset[x]=find(uset[x]);
    return uset[x];
}*/    //遞迴實現路徑壓縮
void unionset(int x,int y)   //按秩合併
{
    int  a=find(x),b=find(y);
    if(a==b)
        return ;
    if(ran[a]>ran[b])
        uset[b]=a;
    else
    {
        if(ran[a]==ran[b])
            ran[b]++;
        uset[a]=b;
    }
}
long long Kruskal(int n,int m)  //Kruskal生成最小生成樹
{
    int i,nedge=0; //nedge 記錄邊的數量 最大為穀倉總數減1 為n-1
    long long res=0;
    sort(s+1,s+1+m,cmp);
    for(i=1;i<=m&&nedge!=n-1;i++)
    {
        if(find(s[i].a)!=find(s[i].b))
        {
            unionset(s[i].a,s[i].b);
            res+=s[i].price;
            nedge++;
        }
    }
    if(nedge<n-1)
        res=-1;
    return res;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        long long ans=0;
        for(int i=1;i<=m;i++)
            scanf("%d%d%lld",&s[i].a,&s[i].b,&s[i].price);
        makeset(n);
        ans=Kruskal(n,m);
        cout<<ans<<endl;
    }
    return 0;
}