您的位置:首页 > 其它

poj2377 最大生成树 Kruskal

2016-04-21 19:14 363 查看
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.

首先是算法选择问题,采取了最保守的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: