Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int e=10;
  7.  
  8. int a,b,u,v,n,i,j,ne=1;
  9. int visited[10]={0},mi,mincost=0,cost[e][e];
  10.  
  11. int smej(int n,int cost[e][e])
  12. {
  13. cout<<"Введiть матрицю сумiжностi\n";
  14.  
  15. for(i=1;i<=n;i++)
  16. for(j=1;j<=n;j++)
  17. {
  18. cin>>cost[i][j];
  19. if(cost[i][j]==0)
  20. cost[i][j]=999;
  21. }
  22. return cost[e][e];
  23. }
  24.  
  25. int minpath(int n,int path[])
  26. {
  27.  
  28. int path_index=0;
  29. while(ne < n)
  30. {
  31. mi =999;
  32. for(i=1;i<=n;i++)
  33. for(j=1;j<=n;j++)
  34. if(cost[i][j]< mi)
  35. if(visited[i]!=0)
  36. {
  37. mi=cost[i][j];
  38. a=u=i;
  39. b=v=j;
  40. }
  41. if(visited[u]==0 || visited[v]==0)
  42. {
  43. path[path_index]=b;
  44. path_index++;
  45. ne++;
  46. mincost+=mi;
  47. visited[b]=1;
  48. }
  49. cost[a][b]=cost[b][a]=999;
  50. }
  51. return *path;
  52. }
  53.  
  54. void out(int n,int path[])
  55. {
  56. cout<<1<<" --> ";
  57. for (int i=0;i<n-1;i++)
  58. {
  59. cout<<path[i];
  60. if (i<n-2) cout<<" --> ";
  61. }
  62.  
  63. cout<<"\n Найменша довжина: "<<mincost<<endl;
  64.  
  65. }
  66.  
  67.  
  68.  
  69. int _tmain(int argc, _TCHAR* argv[])
  70. {
  71. setlocale(LC_ALL,"rus");
  72. int path[100]={0};
  73.  
  74. cout<<"Введiть кiлькiсть вершин"; cin>>n;
  75. **cost=smej(n,cost);
  76. visited[1]=1;
  77. cout<<"\n";
  78. *path=minpath(n,path);
  79. cout<<"\n";
  80. out(n,path);
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement