Advertisement
Guest User

Untitled

a guest
May 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // vaja5warshall
  4. //
  5. // Created by Nejc Drobnic on 21/05/2019.
  6. // Copyright © 2019 Nejc Drobnic. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11.  
  12. using namespace std;
  13.  
  14. int stPov = 0, vel = 0;
  15.  
  16. int D[30][30];
  17. int C[30][30];
  18. int H[30][30];
  19.  
  20. void beri(string f)
  21. {
  22. fstream dat(f.c_str(), fstream::in);
  23. dat >> vel;
  24. int i=0;
  25. int p, q, c;
  26.  
  27. while(!dat.eof())
  28. {
  29. dat >> p >> q >> c;
  30. C[q-1][p-1] = c;
  31. i++;
  32. }
  33. dat.close();
  34. }
  35. //C - matrika sosednosti
  36. //D - matrika najkrajših poti
  37. //H - matrika predhodnikov
  38. void FLOYD_WARSHALL()
  39. {
  40. for(int i = 0; i < vel; i++)
  41. {
  42. for(int j = 0; j < vel; j++)
  43. {
  44. D[i][j] = C[i][j];
  45. if(i != j && C[i][j] != 1000000)
  46. {
  47. H[i][j] = i + 1;
  48. }
  49. else
  50. {
  51. H[i][j] = -1;//-1 ker null ne sprejme int
  52. }
  53. }
  54. }
  55. for(int k = 0; k < vel; k++)
  56. {
  57. for(int i = 0; i < vel; i++)
  58. {
  59. for(int j = 0; j < vel; j++)
  60. {
  61. if(D[i][j] > D[i][k] + D[k][j])
  62. {
  63. D[i][j] = D[i][k] + D[k][j];
  64. H[i][j] = H[k][j];
  65. }
  66. }
  67. }
  68. }
  69. }
  70.  
  71. void IZPIS_POTI(int s, int g)
  72. {
  73. if(s == g)
  74. {
  75. cout << s << endl;
  76. }
  77. else if(H[s - 1][g - 1] == -1)
  78. {
  79. cout << "med s in g ni poti" << endl;
  80. }
  81. else
  82. {
  83. IZPIS_POTI(s, H[s - 1][g - 1]);
  84. cout << g << endl;
  85. }
  86. }
  87.  
  88.  
  89. int main(int argc, const char * argv[])
  90. {
  91. int start, end = 0;
  92. beri("graf.txt");
  93. for(int i = 0; i < vel; i++)
  94. {
  95. for(int j = 0; j < vel; j++)
  96. {
  97. cout << C[i][j] << " ";
  98. }
  99. cout << endl;
  100. }
  101.  
  102. FLOYD_WARSHALL();
  103. cout << "Algoritem zagnan" << endl;
  104.  
  105. cout << "zacetno vozlisce: ";
  106. cin >> start;
  107.  
  108. cout << "koncno vozlice: ";
  109. cin >> end;
  110.  
  111. IZPIS_POTI(start, end);
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement