Pagini recente » Cod sursa (job #1570880) | Cod sursa (job #2758848) | Cod sursa (job #1641940) | Cod sursa (job #1746988) | Cod sursa (job #2564044)
#include <fstream>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int maxx = 400005;
int n, m;
int cost;
struct muchii
{
int x, y, c;
};
muchii muc[maxx];
int t[maxx/2], rang[maxx/2];
vector < pair < int , int > > arbore;
bool sortare(muchii m1, muchii m2)
{
if(m1.c > m2.c)
return false;
return true;
}
int Root(int k)
{
if(t[k] == 0)
return k;
else
{
int x = Root(t[k]);
t[k] = x;
return x;
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
fin >> muc[i].x >> muc[i].y >> muc[i].c;
}
sort(muc + 1, muc + m + 1, sortare);
/*for(int i = 1; i <= m; i++)
{
cout << muc[i].c << " ";
}*/
int cnt = 0;
for(int i = 1; i <= m && cnt < n; i++)
{
int rv1 = Root(muc[i].x), rv2 = Root(muc[i].y);
if(rv1 != rv2)
{
cnt++;
cost += muc[i].c;
arbore.push_back(make_pair(muc[i].x, muc[i].y));
if(rang[rv1] > rang[rv2])
{
t[rv2] = rv1;
}
else
{
t[rv1] = rv2;
if(rang[rv1] == rang[rv2])
{
rang[rv2]++;
}
}
}
}
fout << cost << " " << arbore.size() << endl;
for(int i = 0; i < arbore.size(); i++)
{
fout << arbore[i].first << " " << arbore[i].second << endl;
}
return 0;
}