Pagini recente » Cod sursa (job #1994171) | Cod sursa (job #2958524) | Cod sursa (job #2069499) | Cod sursa (job #3168367) | Cod sursa (job #1487258)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
vector <pair<int, pair<int, int> > > v;
vector <pair <int, int> > p;
int n, m, s, c[200205], nr;
void citire(){
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int cost, nod1, nod2;
fin >> nod1 >> nod2 >> cost;
v.push_back(make_pair(cost, make_pair(nod1, nod2)));
}
sort(v.begin(), v.end());
for(int i=1; i<=n; i++)
c[i] = i;
}
void afisare(){
for(vector<pair<int, int> > ::iterator it = p.begin(); it != p.end(); it++)
fout<<it->first<<" "<< it->second <<" \n";
}
void apm()
{
s = 0;
for(vector <pair <int, pair <int, int> > > ::iterator it = v.begin(); it != v.end(); it++)
{
if(nr == n)
break;
if(c[it->second.first] != c[it->second.second])
{
nr++;
int x = c[it->second.second];
for(int i=1; i<=n; i++)
if(c[i] == x)
c[i] = c[it->second.first];
c[it->second.second] = c[it->second.first];
s += it->first;
p.push_back(make_pair(it->second.second, it->second.first));
}
}
fout<<s<<"\n"<<nr<<"\n";
}
int main()
{
citire();
apm();
afisare();
return 0;
}