Advertisement
tepyotin2

The distance between nodes

Jun 13th, 2025
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int con[1001][1001];
  7. vector<vector<int>> dp;
  8.  
  9. int main(){
  10.     //freopen("distbetweennodes.in", "r", stdin);
  11.    
  12.     cin >> n;
  13.     dp.resize(n, vector<int>(n, 1e9));
  14.     for(int i=0; i<n; i++){
  15.         string s;
  16.         cin >> s;
  17.         for(int j=0; j<n; j++){
  18.             if(s[j]-'0' == 1){
  19.                 dp[i][j] = s[j]-'0';
  20.             }
  21.             //cout << dp[i][j] << " ";
  22.         }
  23.         //cout << '\n';
  24.     }
  25.     //for(int i=0; i<n; i++){
  26.         //for(int j=0; j<n; j++){
  27.             //cout << dp[i][j] << '\n';
  28.         //}
  29.     //}
  30.     //dp[0][0] = 0;
  31.     for(int i=0; i<n; i++){
  32.         dp[i][i] = 0;
  33.     }
  34.     for(int k=0; k<n; k++){
  35.         for(int i=0; i<n; i++){
  36.             for(int j=0; j<n; j++){
  37.                 dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);
  38.             }
  39.         }
  40.     }
  41.     for(int i=0; i<n; i++){
  42.         string s = "";
  43.         for(int j=0; j<n; j++){
  44.             cout << s << dp[i][j];
  45.             s = " ";
  46.         }
  47.         cout << '\n';
  48.     }
  49.    
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement