救命!CE
查看原帖
救命!CE
326796
WRTLEO2楼主2020/4/28 11:38

帮我康康,为什么出来是一堆乱码

康康这里

代码,在线等,急

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
struct edge{
    int u,v,w;	
};
struct edge e[100010];//记录边的信息 
bool cmp(edge x,edge y){
	return x.w<y.w;
}
int f[10001];//记录节点的信息 
int getf(int v){
	if(v!=f[v]) f[v]=getf(f[v]);
	return f[v];
}
int merge(int u,int v){
	int t1=getf(u);
	int t2=getf(v);
	if(t1!=t2){
		f[t1]=t2;
	}
}
int main(){
	int m,n,maxx=999999999;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
	}
	sort(n,n+m+1,cmp);
	for(int i=1;i<=n;i++){
		f[i]=i;
	}
	int count=0;
	for(int i=1;i<=m;i++){
		if(getf(e[i].u)!=getf(e[i].v)){
			maxx=e[i].w;//更新最大值 
			merge(e[i].u,e[i].v);
			count++;
		}
		if(count==n-1) break;
	}
	cout<<n-1<<" "<<maxx;
	return 0;
}
2020/4/28 11:38
加载中...