Advertisement
Guest User

APM

a guest
Oct 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5. int m, n;
  6. struct muchie{int vf1,vf2, cost;}x[100];
  7. ifstream in("grafuri.txt");
  8.  
  9. void citire ()
  10. {
  11.     in>>n>>m;
  12.     for(int i=1;i<=m;i++)
  13.      in>>x[i].vf1>>x[i].vf2>>x[i].cost;
  14. }
  15.  
  16. void sortare()
  17. {
  18.     for(int i=1;i<=m;i++)
  19.         for(int j=1;j<=m;j++)
  20.             if(x[i].cost>x[j].cost)
  21.             swap(x[i],x[j]);
  22. }
  23.  
  24. void APM()
  25. {int c[20],i,k,v1,v2,comp1,comp2;
  26.     for(i=1;i<=n;i++)
  27.         c[i]=i;
  28.     sortare();
  29.     k=0;
  30.     i=1;
  31.     while(k<n-1)
  32.     {
  33.         v1=x[i].vf1;
  34.         v2=x[i].vf2;
  35.         if(c[v1]!=c[v2])
  36.         {
  37.             cout<<x[i].vf1<<" "<<x[i].vf2<<" "<<x[i].cost<<endl;
  38.             k++;
  39.             comp1=c[v1];
  40.             comp2=c[v2];
  41.             for(int j=1;j<=n;j++)
  42.                 if(c[j]==comp2)
  43.                 c[j]=comp1;
  44.         }
  45.         i++;
  46.     }
  47.  
  48. }
  49. int main()
  50. {
  51.     citire();
  52.     APM();
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement