(poj 2377)Kruskal算法 最大生成树

来源:互联网 发布:剑灵天女捏脸图文数据 编辑:程序博客网 时间:2024/05/24 00:03

这道题只是一道模板题,感到唯一的坑点就是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;}
原创粉丝点击