Iamtui1010

floyd

Nov 11th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<vector>
  4. #include<fstream>
  5. #include<algorithm>
  6.  
  7. #define long long long
  8. #define nln '\n'
  9.  
  10. const long N = 110;
  11.  
  12. using namespace std;
  13.  
  14. // Global variables: f1, n, m, a, b
  15.  
  16. fstream f1;
  17. vector<long> b;
  18. vector<vector<long>> a(N), tra(N);
  19. long n, m;
  20.  
  21. void data()
  22. {
  23.     f1.open("vdanger.inp", ios:: in);
  24.     cin >> n >> m;
  25.     b.resize(m+1, 0);
  26.     for (long i = 1; i <= m; ++i)
  27.         cin >> b[i];
  28.     for (long i = 1; i <= n; ++i)
  29.     {
  30.         a[i].resize(n+1, 0);
  31.         tra[i].resize(n+1, 0);
  32.         for (long j = 1; j <= n; ++j)
  33.             cin >> a[i][j];
  34.     }
  35.     f1.close();
  36. }
  37.  
  38. void floyandwarshall()
  39. {
  40.     for (long i = 1; i <= n; ++i)
  41.         for (long j = 1; j <= n; ++j)
  42.             tra[i][j] = i;
  43.  
  44.     for (long k = 1;  k <= n; ++k)
  45.         for (long i = 1; i <= n; ++i)
  46.             for (long j = 1; j <= n; ++j)
  47.                 if (a[i][k] + a[k][j] < a[i][j])
  48.                 {
  49.                     a[i][j] = a[i][k] + a[k][j];
  50.                     tra[i][j] = tra[k][j];
  51.                 }
  52. }
  53.  
  54. void find()
  55. {
  56.     long res = 0;
  57.     for (long i = 1; i <= m-1; ++i)
  58.         res += a[b[i]][b[i+1]];
  59.     cout << res << nln;
  60.     long sta = 1, fin = 3;
  61.     vector<long> pat;
  62.     while (fin != sta)
  63.     {
  64.         pat.push_back(fin);
  65.         fin = tra[sta][fin];
  66.     }
  67.     pat.push_back(fin);
  68.     reverse(pat.begin(), pat.end());
  69.     /*for (const auto &i : pat)
  70.         cout << i << ' ';
  71.     cout << nln;*/
  72. }
  73.  
  74. void process()
  75. {
  76.     floyandwarshall();
  77.     find();
  78. }
  79.  
  80. int main()
  81. {
  82.     data();
  83.     process();
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment