poj2377 最大生成树 Kruskal

来源:互联网 发布:js 检测分辨率 编辑:程序博客网 时间:2024/06/05 05:28

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 81 2 31 3 72 3 102 4 42 5 83 4 63 5 24 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.

首先是算法选择问题,采取了最保守的Kruskal算法,贪心找最长边,并查集检验就好了。
一开始我使用邻接矩阵存的图,但是之后找最大边的算法爆炸了.所以改为用边集数组存边,直接快排(当然也可以用vector但我不太会用)
需要注意几点,一点是输入问题,可能会出现重边,需要取最大值,我采用的方法是用邻接矩阵辅助存储,用空间换时间。
然后是几点小错误,以后需要注意
第一是程序运行退出,可能是sort越界了
第二,用一个参数的时候一定要检查一下上面有没有更改过
第三,自增运算符最好单独一行,避免出错
下面贴代码

#include<stdio.h>#include<math.h>#include<string.h>#include<limits.h>#include<ctype.h>#include<algorithm>using namespace std;int a[1010][1010],m,n,father[1010];struct Edge{int from;int to;int cost;};Edge b[20000+10];//边集数组存储#define zuida 100000000//不知道能不能过卡数据肯定爆炸bool cmp(Edge a,Edge b){    return(a.cost>b.cost);}int find_ancestor(int x){    int r=x;    while(father[r]!=r)    {        r=father[r];    }    int i=x,j;    while(i!=r)    {        j=father[i];        father[i]=r;        i=j;    }    return r;//路径压缩,把所有的x的父节点的父节点都设置为祖先}void add_bian(int x,int y){   int fx=find_ancestor(x),fy=find_ancestor(y);   if(fx!=fy)       father[min(fx,fy)]=max(fx,fy);}int main(void){    int temp_a,temp_b,temp_c,liantong=1,cnt,total,max_cost,max_i,max_j;    int cntt=0,num=0;//num表示目前存储了多少条边    Edge temp;//用来暂时存储边    cnt=total=0;    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)    for(int j=1;j<=n;j++)    {        a[i][j]=-1;//因为是最大生成树,所以Kruskal算法找的是最长边,赋初值为-1        a[i][i]=0;        father[i]=i;    }    while(m--)//注意以后不能用到m了!!!这里很坑爹    {        scanf("%d%d%d",&temp_a,&temp_b,&temp_c);        if(temp_c>a[temp_a][temp_b])        {            temp.from=temp_a;            temp.to=temp_b;            temp.cost=temp_c;            b[num++]=temp;//到最后num会等于边的条数,但是只有0到num-1存有边        //如果题目边重复出现就会有bug要注意        //保留a数组的目的是剔除重复边(取最大值)        }        a[temp_a][temp_b]=max(a[temp_a][temp_b],temp_c);//最大生成树,找最长边        a[temp_b][temp_a]=a[temp_a][temp_b];        add_bian(temp_a,temp_b);        //为了检测图的连通性,首先将所有边全部加入并查集    }    int temp_father=find_ancestor(1);//第一个元素的father    for(int i=1;i<=n;i++)        if(find_ancestor(i)!=temp_father)            liantong=0;    if(liantong==0)        printf("-1");    else    {        for(int i=1;i<=n;i++)            father[i]=i;//再一次初始化father用于之后Kruskal算法        sort(b,b+num,cmp);//把边集数组排序        while(cnt<n-1)        {            max_i=b[cntt].from;            max_j=b[cntt].to;            cntt++;//自增运算符一定要谨慎使用            if(find_ancestor(max_i)!=find_ancestor(max_j))            {                add_bian(max_i,max_j);                cnt++;                total+=a[max_i][max_j];                //printf("%d to %d\n",max_i,max_j);            }        }        printf("%d",total);    }    return 0;}

0 0
原创粉丝点击