#include<stdio.h> #include<queue> using namespace std; #define MAX 99999 int father[MAX]; struct Edge { int a; int b; int dist; bool operator<(const Edge & e) const { if(dist>e.dist) return true; return false; } }edge[MAX]; void init(int n) { for(int i=1;i<=n;i++) { father[i]=i; } } int find(int x) { while(x!=father[x]) { x=father[x]; } return x; } void merge(int a,int b) { int i=find(a); int j=find(b); if(i!=j) { father[i]=j; } } int main() { int n,m; int a,b,d; Edge tmp; priority_queue<Edge> q; while(scanf("%d%d",&n,&m)!=EOF) { init(n); for(int i=0;i<m;i++) { scanf("%d%d%d",&tmp.a,&tmp.b,&tmp.dist); q.push(tmp); } int sum=0; while(!q.empty()) { tmp = q.top(); //printf("11%d %d %d ",tmp.a,tmp.b,tmp.dist); int a=find(tmp.a); int b=find(tmp.b); if(a!=b) { merge(a,b); printf("%d %d ",tmp.a,tmp.b); sum+=tmp.dist; } q.pop(); } printf("%d ",sum); } getchar(); return 0; }