Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream f("date.in");
  7. ofstream g("date.out");
  8.  
  9. int n,m,viz[100]={0},ct=0;;
  10.  
  11. struct grafm {
  12. int x,y,cost;
  13. };
  14. grafm v[100],aux;
  15.  
  16. void citire()
  17. {
  18. int i;
  19. f>>n>>m;
  20. for(i=1;i<=m;i++)
  21. f>>v[i].x>>v[i].y>>v[i].cost;
  22. }
  23.  
  24. void sortv()
  25. {
  26. int i,j;
  27. for(i=1;i<=m;i++)
  28. for(j=1;j<=m;j++)
  29. if(v[i].cost<v[j].cost)
  30. {
  31. aux=v[i];
  32. v[i]=v[j];
  33. v[j]=aux;
  34. }
  35. }
  36.  
  37. void prim()
  38. {
  39. int i,k;
  40. sortv();
  41. viz[v[1].x]=1; viz[v[1].y]=1;
  42. g<<"("<<v[1].x<<" "<<v[1].y<<")"<<'\n';
  43. ct=v[1].cost;
  44. for(k=1;k<=n-2;k++)
  45. {
  46. i=1;
  47. while(viz[v[i].x]==viz[v[i].y]) i++;
  48. g<<"("<<v[i].x<<" "<<v[i].y<<")"<<'\n';
  49. if(viz[v[i].x]!=0) viz[v[i].y]=1;
  50. else viz[v[i].x]=1;
  51. ct=ct+v[i].cost;
  52. }
  53. g<<'\n'<<ct;
  54. }
  55.  
  56. int main()
  57. {
  58. citire();
  59. prim();
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement