cyalue

Untitled

Dec 5th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #define NODE 20
  4. #define INF 999
  5. using namespace std;
  6.  
  7. double costMat[NODE][NODE];
  8.  
  9.  
  10. //Cost matrix of the graph
  11.  
  12.  
  13. double findV(double _y, double pmax = 0.02) {
  14. double p = 1;
  15. int v = 0;
  16.  
  17. if (_y == 0){
  18. return v;
  19. }
  20. long double chisl = _y;
  21. long double sum = chisl;
  22. while (p > pmax) {
  23. v++;
  24. chisl = (chisl * _y)/v;
  25. sum += chisl;
  26. p = chisl/sum;
  27. }
  28. return v;
  29. }
  30.  
  31. void floydWarshal() {
  32. double cost[NODE][NODE]; //defind to store shortest distance from any node to any node
  33. int next[NODE][NODE];
  34. double next1[NODE][NODE];
  35. double b[NODE][NODE];
  36. double v[NODE][NODE];
  37. int a[NODE][NODE];
  38. int b1[NODE][NODE];
  39. cout << " enter first matrix" << endl;
  40. for (int i = 0; i < NODE; i++) {
  41. for (int j = 0; j < NODE; j++) {
  42. cin >> costMat[i][j];
  43. if (costMat[i][j] == 0 && (i != j)) {
  44. costMat[i][j] = INF;
  45. }
  46. }
  47. }
  48. cout << " enter nagruzki" << endl;
  49. for (int i = 0; i < NODE; i++) {
  50. for (int j = 0; j < NODE; j++) {
  51. cin >> next1[i][j];
  52. }
  53. }
  54.  
  55. for (int i = 0; i < NODE; i++) {
  56. for (int j = 0; j < NODE; j++) {
  57. b[i][j] = 0;
  58. }
  59. }
  60.  
  61.  
  62. for (int i = 0; i < NODE; i++)
  63. for (int j = 0; j < NODE; j++) {
  64. cost[i][j] = costMat[i][j];
  65. }
  66.  
  67. for (int i = 0; i < NODE; i++)
  68. for (int j = 0; j < NODE; j++) {
  69. if(costMat[i][j] != INF)
  70. next[i][j] = j;
  71. else{
  72. next[i][j] = -1;
  73. }
  74. }
  75.  
  76. for (int k = 0; k < NODE; k++) {
  77. for (int i = 0; i < NODE; i++)
  78. for (int j = 0; j < NODE; j++)
  79. if (cost[i][k] + cost[k][j] < cost[i][j]) {
  80. cost[i][j] = cost[i][k] + cost[k][j];
  81. next[i][j] = next[i][k];
  82. }
  83. }
  84.  
  85.  
  86. for (int i = 0; i < NODE; i++) {
  87. for (int j = 0; j < NODE; j++) {
  88. int d = i;
  89. do {
  90. b[d][next[d][j]] += next1[i][j];
  91. d = next[d][j];
  92. } while (d != j);
  93. }
  94. }
  95.  
  96. for (int i = 0; i < NODE; i++) {
  97. for (int j = 0; j < NODE; j++) {
  98. v[i][j] = findV(b[i][j]);
  99. }
  100. }
  101.  
  102. for (int i = 0; i < NODE; i++) {
  103. for (int j = 0; j < NODE; j++) {
  104. a[i][j] = (int)(v[i][j] * 85600);
  105. if (a[i][j] != 0)b1[i][j] = a[i][j] + 16000;
  106. else b1[i][j] = 0;
  107.  
  108. }
  109. }
  110.  
  111.  
  112.  
  113. cout << "The matrix:" << endl;
  114. for (int i = 0; i < NODE; i++) {
  115. for (int j = 0; j < NODE; j++) {
  116. cout.precision(3);
  117. cout << cost[i][j] << " ";
  118. }
  119. cout << endl;
  120. }
  121.  
  122.  
  123. cout << "The matrix:" << endl;
  124. for (int i = 0; i < NODE; i++) {
  125. for (int j = 0; j < NODE; j++) {
  126. cout.precision(3);
  127. cout << setw(5) << b[i][j] << " ";
  128. }
  129. cout << endl;
  130. }
  131.  
  132.  
  133. cout << "The matrix v:" << endl;
  134. for (int i = 0; i < NODE; i++) {
  135. for (int j = 0; j < NODE; j++) {
  136. cout.precision(3);
  137. cout << setw(5) << v[i][j] << " ";
  138. }
  139. cout << endl;
  140. }
  141.  
  142. cout << "The matrix a:" << endl;
  143. for (int i = 0; i < NODE; i++) {
  144. for (int j = 0; j < NODE; j++) {
  145. cout.precision(10);
  146. cout << setw(1) << a[i][j] << " ";
  147. }
  148. cout << endl;
  149. }
  150. cout << "The matrix b:" << endl;
  151. for (int i = 0; i < NODE; i++) {
  152. for (int j = 0; j < NODE; j++) {
  153. cout.precision(10);
  154. cout << setw(1) << b1[i][j] << " ";
  155. }
  156. cout << endl;
  157. }
  158.  
  159.  
  160. cout << " \n \n \n";
  161. for (int i = 0; i < NODE; i++) {
  162. for (int j = 0; j < NODE; j++) {
  163. cout << setw(5) << next[i][j] + 1;
  164. }
  165. cout << endl;
  166. }
  167.  
  168.  
  169. }
  170.  
  171. int main() {
  172. floydWarshal();
  173. }
Advertisement
Add Comment
Please, Sign In to add comment