【POJ - 1797】Heavy Transportation(最大生成树,kruskal,并查集)

描述

You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.

从1到n的路径中最小的边最大

解法

(最小的边最大很容易想到二分)
我们选取一个边权尽量大的路径即可
-》最大生成树
如果用kruskal算法的话,最后一条加入的边(使得1和n成为一个集合)的边权就是答案

代码

#include<cstdio>
#include<algorithm>
typedef long long LL;
const int N=1e3+10; 
const int oo=0x3f3f3f3f; 
using namespace std;
bool flag;
int fa[N],x[N],y[N];
int read(){
	int f=1,s=0;char c=getchar();
	for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
	for(;c>='0'&&c<='9';c=getchar())s=s*10+c-48;
	return f*s;
}
struct qq{
	int u,v,w;
	void in(){
		u=read();v=read();w=read();
	}
}a[N*N];
bool operator<(qq a,qq b){
	return a.w>b.w;
}
int find(int x){
	if(fa[x]!=x)fa[x]=find(fa[x]);
	return fa[x]; 
}
bool check(int x,int y){
	return find(x)==find(y);
}
void join(int x,int y){
	x=find(x);y=find(y);
	if(x==y)return;
	fa[x]=y;
}
int work(){
	int n=read(),m=read();
	for(int i=1;i<=n;i++)fa[i]=i;
	for(int i=0;i<m;i++)a[i].in();
	sort(a,a+m);
	for(int i=0;i<m;i++){
		int u=find(a[i].u),v=find(a[i].v);
		if(u==v)continue;
		join(u,v);
		if(check(1,n)==true)return a[i].w;
	}
}
int main(){
	int T=read();
	for(int i=1;i<=T;i++)
		printf("Scenario #%d:\n%d\n\n",i,work());
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值