社区讨论

Need help!!!

P1111修复公路参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lt86csw4
此快照首次捕获于
2024/03/01 12:48
2 年前
此快照最后确认于
2024/03/01 17:29
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
int n,m,f[10001],ch,times;
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
struct node{
	int x,y,t;
}a[10001];
bool cmp(node a,node b){
	return a.t<b.t;
}
int check(int x,int y){
	if(f[find(x)]==find(y))return 1;
	return 0;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>a[i].x>>a[i].y>>a[i].t;
		f[i]=i;
	}
	sort(a+1,a+m+1,cmp);
	for(int i=1;i<=m;i++){
		f[find(a[i].x)]=find(a[i].y);
		if(check(a[i].x,a[i].y)==1){
			ch=1;
			times=a[i].t;
		}
		else{
			ch=0;
		}
	}
	if(ch==1){
		cout<<times<<endl;
	}
	else cout<<"-1"<<endl;
	return 0;
}
5WA5RE(悲)

回复

1 条回复,欢迎继续交流。

正在加载回复...