Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct muchie{int x, y, c;};
  5. muchie u[100]; int L[100];
  6. ifstream f("graf.in");
  7. int n, m;
  8. void citire()
  9. {
  10. f>>n>>m;
  11. for(int i=1;i<=m;i++)
  12. {
  13. f>>u[i].x>>u[i].y>>u[i].c;
  14. }
  15. for(int i=1;i<=n;i++)
  16. {
  17. L[i]=i;
  18. }
  19. }
  20. void Kruskal()
  21. {
  22. for(int i=1;i<=m;i++)
  23. {
  24. for(int j=i+1;j<=m;j++)
  25. {
  26. if(u[i].c>u[j].c)
  27. swap(u[i], u[j]);
  28. }
  29. }
  30. int k=1, i=1, ct=0;
  31. while(k<=n-1)
  32. {
  33. int xx=u[i].x;
  34. int yy=u[i].y;
  35. if(L[xx]!=L[yy])
  36. {
  37. cout<<xx<<" "<<yy<<" "<<u[i].c<<endl;
  38. ct=ct+u[i].c;
  39. for(int j=1;j<=n;j++)
  40. {
  41. if(L[j]==yy)
  42. {
  43. L[j]=xx;
  44. }
  45. }
  46. k++;
  47. }
  48. i++;
  49. }
  50. cout<<ct;
  51. }
  52. int main()
  53. {
  54. citire();
  55. Kruskal();
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement