egogoboy

Floyd

Jul 14th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. // First task
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main() {
  10.  
  11.     std::ifstream fin("input.txt");
  12.     std::ofstream fout("output.txt");
  13.  
  14.     int n;
  15.     fin >> n;
  16.     vector<vector<int>> m(n, vector<int> (n));
  17.  
  18.     for (int i = 0; i < n; ++i) {
  19.         for (int j = 0; j < n; ++j) {
  20.             fin >> m[i][j];
  21.         }
  22.     }
  23.  
  24.     for (int k = 0; k < n; ++k) {
  25.         for (int i = 0; i < n; ++i) {
  26.             for (int j = 0; j < n; ++j) {
  27.            
  28.                 m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
  29.                
  30.             }
  31.         }
  32.     }
  33.  
  34.     for (int i = 0; i < n; ++i) {
  35.         for (int j = 0; j < n; ++j) {
  36.             fout << m[i][j] << " ";
  37.         }
  38.         fout << endl;
  39.     }
  40.     return 0;
  41.  
  42. }
  43.  
  44. // Second task
  45.  
  46. #include <iostream>
  47. #include <fstream>
  48. #include <vector>
  49. #include <string>
  50. using namespace std;
  51.  
  52. int main() {
  53.  
  54.     std::ifstream fin("input.txt");
  55.     std::ofstream fout("output.txt");
  56.  
  57.     int n;
  58.     fin >> n;
  59.     vector<vector<int>> m(n, vector<int>(n));
  60.  
  61.     for (int i = 0; i < n; ++i) {
  62.         for (int j = 0; j < n; ++j) {
  63.             fin >> m[i][j];
  64.         }
  65.     }
  66.  
  67.     int max = -1;
  68.  
  69.     for (int k = 0; k < n; ++k) {
  70.         for (int i = 0; i < n; ++i) {
  71.             for (int j = 0; j < n; ++j) {
  72.  
  73.                 if (m[i][k] != -1 && m[k][j] != -1)
  74.                     if (m[i][j] == -1) {
  75.                         m[i][j] = m[i][k] + m[k][j];
  76.                     }
  77.                     else {
  78.                         m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
  79.                     }
  80.  
  81.             }
  82.         }
  83.     }
  84.  
  85.     for (int i = 0; i < n; ++i) {
  86.         for (int j = 0; j < n; ++j) {
  87.  
  88.             if (m[i][j] > max)
  89.                 max = m[i][j];
  90.         }
  91.     }
  92.  
  93.     fout << max;
  94.     return 0;
  95.  
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment