Pagini recente » Cod sursa (job #1833359) | Cod sursa (job #3151150) | Cod sursa (job #1221125) | Cod sursa (job #3292581) | Cod sursa (job #2502460)
#include <iostream>
#include <fstream>
#include <algorithm>
#define NMAX 200000
#define MMAX 400000
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie {
int x,y,c;
}v[MMAX+1];
struct graf {
int x,y;
}arbore[NMAX+1];
int n,m,cost=0,tata[NMAX+1];
bool sortare(muchie x,muchie y) {
return x.c<y.c;
}
int sef(int x) {
if(x==tata[x])
return x;
else
return tata[x]=sef(tata[x]);
}
void unire(int x,int y) {
int sefx=sef(x);
int sefy=sef(y);
tata[sefx]=sefy;
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++) {
if(i<=n)
tata[i]=i;
fin>>v[i].x>>v[i].y>>v[i].c;
}
sort(v+1,v+m+1,sortare);
int k=0;
for(int i=1;i<=m && k<n;i++) {
if(sef(v[i].x)!=sef(v[i].y)) {
unire(v[i].x,v[i].y);
cost+=v[i].c;
k++;
arbore[k].x=v[i].x;
arbore[k].y=v[i].y;
}
}
fout<<cost<<'\n'<<n-1<<'\n';
for(int i=1;i<n;i++)
fout<<arbore[i].x<<" "<<arbore[i].y<<'\n';
return 0;
}