poj 2395 MST的最大边(水)

点击打开链接

题意:从源点能到任意一点且 每走1单位长度就要消耗一单位水,每到一个农场可以把自己的水充满,求至少要带的水。

 思路:图要是连通的&&每条边尽量小 (若已经连通 在加边的只可能增大ans) -》 求MST的最大边即可

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int M = 51010;
struct edge{
	long u;
	long v;
	long long cost;
	bool operator <(const edge &t)const
	{
		return cost<t.cost; 
	}
}e[M];
long n,p,m,fa[M];
void Inin()
{
	for(int i=1;i<n;i++) //
	{
		fa[i]=i;
	}
}
int find(int x)
{
	if(x!=fa[x])
	{
		fa[x]=find(fa[x]); //路径压缩		
	}
	return fa[x];
	
}
void Union(int x,int y)
{
	int a=find(x);
	int b=find(y);
	if(a!=b)
	{
		fa[a]=b;
	}
}
void Kruskal()
{
	Inin();// union-set 初始化 
	long long ans=0;
	for(int i=0;i<m;i++)
	{
		if(find(e[i].u)!=find(e[i].v))
		{
			Union(e[i].u,e[i].v);
			ans=max(ans,e[i].cost);
		}				
		
	}
	cout<<ans<<endl;

}
int main() 
{

	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		
		scanf("%d%d%ld",&e[i].u,&e[i].v,&e[i].cost);
	}
	sort(e,e+m);
	Kruskal(); //每次将两个连通分量合并成一个时  花费最小 
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值