Advertisement
hosamkora

Untitled

Nov 13th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #define size 6
  4. #define inf 100
  5. using namespace std;
  6.  
  7. int main(void) {
  8. int graph[size][size] ={ { 0 , 30 , 40 , inf , inf , inf }
  9. , { 30 , 0 , inf , 6 , inf , 1 }
  10. , { 40 , inf , 0 , inf , 5 , inf }
  11. , { inf , 6 ,inf , 0 , 2 , 1 }
  12. ,{ inf , inf , 5 , 2 , 0 , inf }
  13. ,{ inf , 1 , inf , 1 , inf , 0 }};
  14.  
  15. int stateArray[size][3];
  16. int source = 0 ;
  17. int destination = 3;
  18. int minV = -1;
  19. int minW = -1;
  20. int c = 0;
  21.  
  22. for(c = 0 ; c< size ; c++){
  23. stateArray[c][0]= -1; // pre-vertex
  24. stateArray[c][1]= inf;//sum weights from pre-vertices
  25. stateArray[c][2]= 0;// state - visited vertex or not
  26. }
  27.  
  28.  
  29. minV = source;
  30. minW = graph[source][source];
  31. stateArray[source][0] = source;
  32. stateArray[source][1] = 0;
  33. stateArray[source][2] = 1;
  34.  
  35. while(minV != destination){
  36. for(c = 0 ; c< size ; c++ ){
  37. if( stateArray[c][2] == 0 && stateArray[c][1] > graph[minV][c]+minW){
  38. stateArray[c][1] = graph[minV][c]+minW;
  39. stateArray[c][0] = minV ;
  40. }
  41.  
  42. }
  43.  
  44. minV = -1 ;
  45. minW = inf ;
  46.  
  47. for(c = 0 ; c < size ; c++){
  48. if(stateArray[c][2] == 0 && stateArray[c][1] < minW ){
  49. minW = stateArray[c][1];
  50. minV = c;
  51. }
  52. }
  53.  
  54. stateArray[minV][2]=1;
  55. }
  56.  
  57. cout<<destination << "\t";
  58. c = destination;
  59. while(c != source){
  60. cout<<stateArray[c][0]<<"\t";
  61. c = stateArray[c][0];
  62. }
  63.  
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement