Pagini recente » Cod sursa (job #1437236) | Cod sursa (job #2260423) | Cod sursa (job #3142927) | Cod sursa (job #2313536) | Cod sursa (job #2401415)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
struct muchie{
int x,y,cost;
}v[400001],t[200001];
int n,m,s[400001],ct,cont;
void citire()
{
in>>n>>m;
for(int i=1;i<=m;i++)
{
in>>v[i].x>>v[i].y>>v[i].cost;
}
}
void kruskal()
{
int a,b;
for(int i=1;i<=n;i++)
s[i] = i;
for(int i=1;i<=m;i++)
{
a=s[v[i].x];
b=s[v[i].y];
if(a!=b)
{
ct+=v[i].cost;
cont++;
t[cont].x = v[i].x;
t[cont].y = v[i].y;
for(int j=1;j<=n;j++)
{
if(s[j] == b)
s[j] = a;
}
}
}
}
bool cmp(muchie st, muchie dr)
{
return st.cost<dr.cost;
}
int main()
{
citire();
sort(v+1,v+m+1,cmp);
kruskal();
out<<ct<<'\n'<<cont<<'\n';
for(int i=1;i<=cont;i++)
{
out<<t[i].x<<' '<<t[i].y<<'\n';
}
return 0;
}