Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define INF 1000000000
- using namespace std;
- unsigned** accessibility(unsigned **matrix, unsigned n)
- {
- unsigned **result = new unsigned*[n];
- unsigned i, j;
- for(i = 0; i < n; ++i)
- {
- result[i] = new unsigned[n];
- for(j = 0; j < n; ++j)
- {
- result[i][j] = (matrix[i][j] == 0 ? INF : matrix[i][j]);
- }
- }
- unsigned k;
- for(k = 0; k < n; ++k)
- {
- for(i = 0; i < n; ++i)
- {
- for(j = 0; j < n; ++j)
- {
- result[i][j] = min(result[i][j], result[i][k] + result[k][j]);
- }
- }
- }
- for(i = 0; i < n; ++i)
- {
- for(j = 0; j < n; ++j)
- {
- if(result[i][j] == INF)
- result[i][j] = 0;
- }
- }
- return result;
- }
- int main()
- {
- unsigned n;
- cin >> n;
- unsigned **matrix = new unsigned*[n];
- unsigned i, j;
- for(i = 0; i < n; ++i)
- {
- matrix[i] = new unsigned[n];
- for(j = 0; j < n; ++j)
- {
- cin >> matrix[i][j];
- }
- }
- unsigned **result = accessibility(matrix, n);
- for(i = 0; i < n; ++i)
- {
- for(j = 0; j < n; ++j)
- {
- cout << result[i][j] << " ";
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement