算是模版了 没什么限制 结束输出就行了
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<stdlib.h> 7 using namespace std; 8 #define N 1010 9 #define M 15010 10 struct node 11 { 12 int x,y,len; 13 }p[M]; 14 int fa[N]; 15 int find(int x) 16 { 17 if(fa[x]!=x) 18 fa[x] = find(fa[x]); 19 return fa[x]; 20 } 21 bool cmp(node a,node b) 22 { 23 return a.len<b.len; 24 } 25 int main() 26 { 27 int i,j,n,m; 28 scanf("%d%d",&n,&m); 29 for(i =1; i <= n ; i++) 30 fa[i] = i; 31 for(i = 1 ; i <= m ; i++) 32 { 33 scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].len); 34 } 35 sort(p+1,p+m+1,cmp); 36 int s = 0,maxz=0; 37 for(i = 1; i <= m&&s<n-1 ; i++) 38 { 39 int tx = find(p[i].x),ty = find(p[i].y); 40 if(tx!=ty) 41 { 42 fa[tx] = ty; 43 s++; 44 maxz = max(maxz,p[i].len); 45 } 46 } 47 printf("%d\n",maxz); 48 printf("%d\n",i-1); 49 for(j = 1; j < i ; j++) 50 printf("%d %d\n",p[j].x,p[j].y); 51 return 0; 52 }