Pagini recente » Monitorul de evaluare | Cod sursa (job #1099432) | Cod sursa (job #2530670) | Cod sursa (job #1064610) | Cod sursa (job #2206519)
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
vector<int> tata;
vector<pair<pair<int,int>,int>> m,fn;
int Find (int x)
{
if(x!=tata[x])
x=tata[x];
return x;
}
void Union (int x, int y)
{
int a=Find(x);
int b=Find(y);
tata[a]=b;
}
bool cmp(pair<pair<int,int>,int> a, pair<pair<int,int>,int> b)
{
return a.second<b.second;
}
int n,mm;///n este numarul de noduri,iar mm este numarul de muchii
int main()
{
int i,a,b,c,nr=0,cost=0;
f>>n>>mm;
for(i=0; i<=n; i++)
tata.push_back(i);
for(i=1; i<=mm; i++)
{
f>>a>>b>>c;
m.push_back({{a,b},c});
}
f.close();
sort(m.begin(),m.end(),cmp);
i=0;
while(i!=m.size())
{
if(Find(m[i].first.first) != Find(m[i].first.second))
{
nr++;
fn.push_back(m[i]);
cost+=m[i].second;
Union(m[i].first.first,m[i].first.second);
}
i++;
}
g<<cost<<endl<<nr<<endl;
for(i=0; i<fn.size(); i++)
{
g<<fn[i].first.first<<" "<<fn[i].first.second<<endl;
}
g.close();
return 0;
}