您的位置:首页 > 其它

ZOJ 1542 / POJ 1861 Network (kruskal )

2010-09-15 13:48 411 查看
又是一个MST得水题,受不了。

对于让最大边最小,显然可以用kruskal从最小边慢慢添加。

真的没有什么可以解释的了。。。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int p[1001];
int find(int i)
{   return i==p[i]?i:(p[i]=find(p[i]));}
struct edge
{
int from,to;
int w;
} a[15001],t;
int cmp( const void *a,const void *b)
{
struct edge aa = *(struct edge *)a;
struct edge bb = *(struct edge *)b;
if( aa.w > bb.w ) return 1;
if( aa.w < bb.w ) return -1;
return 0;
}
int main(void)
{
int n,m,max,count;
int i,j,from,to;
int jilu[1001];
while( scanf("%d%d",&n,&m) != EOF )
{
for( i = 1; i <= n; i++ ) p[i] = i;
for( i = 1; i <= m; i++ )
{
scanf("%d%d%d",&t.from,&t.to,&t.w);
a[i] = t;
}

qsort(a+1,m,sizeof( a[0] ) ,cmp);

for( i = 1,count = 0,max=-1; i <= m; i++  )
{
//printf("%d/n",a[i].w);
from = find(a[i].from);
to = find(a[i].to);
if( from == to ) continue;
p[from] = to;
jilu[++count] = i;
if( a[i].w > max ) max = a[i].w;
}

printf("%d/n%d/n",max,count);
for( i = 1; i <= count; i++)
{
printf("%d %d/n",a[ jilu[i] ].from,a[ jilu[i] ].to);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: