16pts HELP
查看原帖
16pts HELP
1173109
OrientDragon楼主2024/10/4 15:06
#include <bits/stdc++.h>
#define int long long
int read(int x=0){
	char ch=getchar();
	while(ch<48||ch>57)ch=getchar();
	while(ch>=48&&ch<=57){
		x=x*10+(ch^48);
		ch=getchar();
	}
	return x;
}
void write(int x){
	if(x>9)write(x/10);
	putchar(x%10^48);
}
struct edge{
	int u,v,w;
	bool operator<(const edge&x)const{
		return w<x.w;
	}
}a[200005];
int fa[5005],n,m,cnt,ans,now=1;
int gf(int x){return fa[x]==x?x:fa[x]=gf(fa[x]);}
main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++)fa[i]=i;
	for(int i=1;i<=m;i++)a[i].u=read(),a[i].v=read(),a[i].w=read();
	std::sort(a+1,a+n+1);
	while(cnt<n-1&&now<=m){
		int tmp1=gf(a[now].u),tmp2=gf(a[now].v);
		if(tmp1!=tmp2)cnt++,ans+=a[now].w,fa[tmp1]=tmp2;
		now++;
	}
	if(cnt==n-1)write(ans);
	else puts("orz");
}

不知道为什么加上 orz 之后就全输出 orz 了。。。

2024/10/4 15:06
加载中...