Pagini recente » Cod sursa (job #1878416) | Cod sursa (job #2752492) | Clasament bulangandit1 | Cod sursa (job #2728702) | Cod sursa (job #1125633)
#include<fstream>
#include<algorithm>
#define nx 200007
using namespace std;
struct gr{
int x;
int y;
int c;
}a[nx];
int n,m,i,j,x,y,c,l,viz[nx],v1[nx],v2[nx];
ifstream fin("apm.in");
ofstream fout("apm.out");
int cmp(const gr a1,const gr b)
{
return a1.c<b.c;
}
void citire()
{
fin>>n>>m;
for(i=1;i<=m;i++){
fin>>x>>y>>c;
a[i].x=x;
a[i].y=y;
a[i].c=c;
viz[i]=i;
}
}
int main()
{
citire();
sort(a+1,a+m+1,cmp);
j=0;i=0;c=0;
while (i<=m && j!=n-1){
i++;
x=a[i].x;
y=a[i].y;
if(viz[x]!=viz[y]){
j++;
c=c+a[i].c;
v1[j]=x;
v2[j]=y;
x=viz[x];
y=viz[y];
for(l=1;l<=n;l++)
if(viz[l]==y)viz[l]=x;
}
}
fout<<c<<'\n';
fout<<n-1<<'\n';
for(i=1;i<=n-1;i++)fout<<v1[i]<<' '<<v2[i]<<'\n';
return 0;
}