Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int m, n;
- struct muchie{int vf1,vf2, cost;}x[100];
- ifstream in("grafuri.txt");
- void citire ()
- {
- in>>n>>m;
- for(int i=1;i<=m;i++)
- in>>x[i].vf1>>x[i].vf2>>x[i].cost;
- }
- void sortare()
- {
- for(int i=1;i<=m;i++)
- for(int j=1;j<=m;j++)
- if(x[i].cost>x[j].cost)
- swap(x[i],x[j]);
- }
- void APM()
- {int c[20],i,k,v1,v2,comp1,comp2;
- for(i=1;i<=n;i++)
- c[i]=i;
- sortare();
- k=0;
- i=1;
- while(k<n-1)
- {
- v1=x[i].vf1;
- v2=x[i].vf2;
- if(c[v1]!=c[v2])
- {
- cout<<x[i].vf1<<" "<<x[i].vf2<<" "<<x[i].cost<<endl;
- k++;
- comp1=c[v1];
- comp2=c[v2];
- for(int j=1;j<=n;j++)
- if(c[j]==comp2)
- c[j]=comp1;
- }
- i++;
- }
- }
- int main()
- {
- citire();
- APM();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement