Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include<iostream.h>
  6. #include<string.h>
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. #include<conio.h>
  10. #define word unsigned int
  11.  
  12. //---------------------------------------------------------------------------
  13.  
  14.  
  15. int a, b, u, v, n, i, j, ne=1;
  16. int visited[10]={0}, m, mincost=0, cost[10][10];
  17.  
  18. void main()
  19. {
  20. int path[100]={0}; //
  21. int path_index=0;
  22.  
  23. clrscr();
  24. cout<<"VVedite kolichestvo vershin "; cin>>n;
  25. cout<<"vvedite matritsy rasstoyaniu\n";
  26.  
  27.  
  28.  
  29. for(i=1;i<=n;i++)
  30. for(j=1;j<=n;j++)
  31. {
  32. cin>>cost[i][j];
  33. if(cost[i][j]==0)
  34. cost[i][j]=999; //999 }
  35. visited[1]=1;
  36. cout<<"\n";
  37.  
  38. while(ne < n)
  39. {
  40. for(i=1, m=999; i<=n;i++)
  41. for(j=1; j<=n; j++)
  42. if(cost[i][j]< m)
  43. if(visited[i]!=0)
  44. {
  45. m=cost[i][j];
  46. a=u=i;
  47. b=v=j;
  48. }
  49. if(visited[u]==0 || visited[v]==0)
  50. {
  51. path[path_index]=b;
  52. path_index++;
  53. //cout<<"\n "<<ne++<<" "<<a<<" "<<b<<min; //
  54. ne++; //
  55. mincost+=m;
  56. visited[b]=1;
  57.  
  58. }
  59. cost[a][b]=cost[b][a]=999;
  60. }
  61.  
  62.  
  63. cout<<"\n";
  64.  
  65. cout<<1<<" --> ";
  66. for (int i=0;i<n-1;i++)
  67. {
  68. cout<<path[i];
  69. if (i<n-2) cout<<" --> ";
  70. }
  71.  
  72. cout<<"\n minimum rasstouanie "<<mincost;
  73.  
  74.  
  75. cin.get();
  76. cin.get();
  77. }
  78. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement