Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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(tata[x]==x)
- return x;
- tata[x]=Find(tata[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);
- /*for (i=0; i<m.size(); i++)
- cout<<m[i].first.first<<" "<<m[i].first.second<<" "<<m[i].second<<endl;*/
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement