miffy_123 @ 2024-02-13 20:48:16
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 2147483646
#define inf 9223372036854775806
#define pl putchar('\n');
const int maxn=2505;
const int maxm=200005;
int n,m;
struct line__{
int startpoint__,finishpoint__,weight__;
}l[maxm];
namespace fjset{
int p[maxn];
void init(int n=2500){
for(int i=0;i<=n;i++){
p[i]=i;
}
return;
}
int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
void join(int x,int y){
x=find(x);
y=find(y);
if(x==y)return;
p[x]=y;
return;
}
bool check(int x,int y){
x=find(x);
y=find(y);
return (x==y);
}
}
int read(){
int f=1,k=0;
char c;
c=getchar();
while(c<'0'||c>'9'){
if(c=='-'){
f=-1;
}
c=getchar();
}
while(c>='0'&&c<='9'){
k=(k<<1)+(k<<3)+(c^48);
c=getchar();
}
return f*k;
}
void write(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9){
write(x/10);
}
putchar(x%10+'0');
}
bool cmp(line__ &A,line__ &B){
if(A.weight__==B.weight__){
if(A.startpoint__==B.startpoint__){
return A.finishpoint__<B.finishpoint__;
}
return A.startpoint__<B.startpoint__;
}
return A.weight__<B.weight__;
}
int main(){
n=read();
m=read();
fjset::init();
for(register int i=0;i<m;i++){
l[i].startpoint__=read();
l[i].finishpoint__=read();
l[i].weight__=read();
}
int k=n-1;
sort(l,l+n,cmp);
int sum=0;
for(register int i=0;i<m&&k>0;i++){
if((fjset::check(l[i].startpoint__,l[i].finishpoint__))==false){
k--;
sum+=l[i].weight__;
fjset::join(l[i].startpoint__,l[i].finishpoint__);
}
}
for(int i=1;i<=n;i++){
if((fjset::check(1,i))==false){
puts("orz");
return 0;
}
}
write(sum);
return 0;
}
by huyangmu @ 2024-02-13 20:54:50
第一个错误,排序应该是
sort(l,l+m,cmp);
by miffy_123 @ 2024-02-13 20:54:54
@s11304
by miffy_123 @ 2024-02-13 20:55:41
thanks,已关
by miffy_123 @ 2024-02-13 20:57:28
已过,谢谢@HuYangMu2011 ,此帖结