POJ - 1861 Network (Kruskal)

题干:

给你n个点和m条边及其权值,求一个能使图联通且单条边权最大值最小的边的集合。
输出边权的最大值,边的个数,边的起始点。

思路:

Kruskal求最小生成树模板题,记录下最大边权。

#include <cstdio>  
#include <cstring>
#include <iostream>    
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
int n,m,tot,f[32000],ans[2000],ans1[2000],mx=-1,sum;
struct stu
{
	int u;
	int v;
	int cost;
}edge[32000];
void add(int a,int b,int c)
{
	edge[tot].u=a;
	edge[tot].v=b;
	edge[tot++].cost=c;
}
bool cmp(stu a,stu b)
{
	return a.cost<b.cost;
}
int find(int a)
{
	if(f[a]==-1)	return a;
	else	return f[a]=find(f[a]);	
}
int main()
{
	int a,b,c;
	memset(f,-1,sizeof(f));
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
		//add(b,a,c);
	}
	sort(edge,edge+tot,cmp);
	for(int i=0;i<m;i++){
		int u=edge[i].u;
		int v=edge[i].v;
		int cost=edge[i].cost;
		int t1=find(u),t2=find(v);
		if(t1!=t2)
		{
			f[t1]=t2;
			ans[sum]=u;
			ans1[sum++]=v;
			if(cost>mx)
				mx=cost;
		}
	}
	printf("%d\n%d\n",mx,sum);
	for(int i=0;i<sum;i++){
		printf("%d %d\n",ans[i],ans1[i]);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值