Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #define INF 1000000000
  3.  
  4. using namespace std;
  5.  
  6. unsigned** accessibility(unsigned **matrix, unsigned n)
  7. {
  8. unsigned **result = new unsigned*[n];
  9.  
  10. unsigned i, j;
  11. for(i = 0; i < n; ++i)
  12. {
  13. result[i] = new unsigned[n];
  14.  
  15. for(j = 0; j < n; ++j)
  16. {
  17. result[i][j] = (matrix[i][j] == 0 ? INF : matrix[i][j]);
  18. }
  19. }
  20.  
  21. unsigned k;
  22. for(k = 0; k < n; ++k)
  23. {
  24. for(i = 0; i < n; ++i)
  25. {
  26. for(j = 0; j < n; ++j)
  27. {
  28. result[i][j] = min(result[i][j], result[i][k] + result[k][j]);
  29. }
  30. }
  31. }
  32.  
  33. for(i = 0; i < n; ++i)
  34. {
  35. for(j = 0; j < n; ++j)
  36. {
  37. if(result[i][j] == INF)
  38. result[i][j] = 0;
  39. }
  40. }
  41.  
  42. return result;
  43. }
  44.  
  45. int main()
  46. {
  47. unsigned n;
  48.  
  49. cin >> n;
  50.  
  51. unsigned **matrix = new unsigned*[n];
  52.  
  53. unsigned i, j;
  54. for(i = 0; i < n; ++i)
  55. {
  56. matrix[i] = new unsigned[n];
  57.  
  58. for(j = 0; j < n; ++j)
  59. {
  60. cin >> matrix[i][j];
  61. }
  62. }
  63.  
  64. unsigned **result = accessibility(matrix, n);
  65.  
  66. for(i = 0; i < n; ++i)
  67. {
  68. for(j = 0; j < n; ++j)
  69. {
  70. cout << result[i][j] << " ";
  71. }
  72.  
  73. cout << endl;
  74. }
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement