Pagini recente » Cod sursa (job #262610) | Cod sursa (job #2144065) | Cod sursa (job #2305538) | Cod sursa (job #145748) | Cod sursa (job #3340605)
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std;
struct muchie
{
int i, j, c;
bool ok;
};
ifstream f("apm.in");
ofstream g("apm.out");
int n, m, op, CC[200000], /*NrM[101],*/ k, nm;
long long cost;
muchie M[400001];
int Find(int i)
{
if(CC[i]==0)
return i;
return CC[i]=Find(CC[i]);
}
void citire()
{
f>>n>>m;
for(int i=1; i<=m; i++)
{
f>>M[i].i>>M[i].j>>M[i].c;
}
}
bool comp(const muchie &a, const muchie &b)
{
return a.c<b.c;
}
void Kruskal()
{
sort(M+1, M+m+1, comp);
for(int i=1; i<=m; i++)
{
int ci=Find(M[i].i);
int cj=Find(M[i].j);
if(ci!=cj)
{
nm++;
cost+=M[i].c;
CC[ci]=cj;
// NrM[++nm]=i;
M[i].ok=1;
if(nm==n-1) break;
}
}
}
int main()
{
citire();
Kruskal();
g<<cost<<'\n'<<nm<<'\n';
for(int i=1; i<=m; i++)
if(M[i].ok)
g<<M[i].i<<' '<<M[i].j<<'\n';
return 0;
}