Advertisement
Guest User

B

a guest
Apr 6th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = (int) 5e1;
  6. const int inf = (int) 1e9;
  7.  
  8. struct Matrix {
  9.     vector<vector<int>> mat;
  10.  
  11.     Matrix() {
  12.  
  13.     }
  14.  
  15.     Matrix(int n) {
  16.         mat = vector<vector<int>>(n, vector<int>(n, inf));
  17.     }
  18.    
  19.     Matrix(vector<vector<int>> _mat) {
  20.         mat = _mat;
  21.     }
  22.  
  23.     void print() {
  24.         int n = mat.size();
  25.  
  26.         for (int i = 0; i < n; i++) {
  27.             for (int j = 0; j < n; j++) {
  28.                 printf("%d%c", mat[i][j], " \n"[j + 1 == n]);
  29.             }
  30.         }
  31.     }
  32.  
  33.     friend Matrix operator * (Matrix a, Matrix b) { // a, b - square matrices
  34.         int n = a.mat.size();      
  35.         Matrix c = Matrix(n);
  36.  
  37.         for (int i = 0; i < n; i++) {
  38.             for (int j = 0; j < n; j++) {
  39.                 for (int k = 0; k < n; k++) {
  40.                     if (a.mat[i][k] == inf || b.mat[k][j] == inf) {
  41.                         continue;
  42.                     }
  43.  
  44.                     int v = a.mat[i][k] + b.mat[k][j];
  45.  
  46.                     if (k == 0) {
  47.                         c.mat[i][j] = v;
  48.                     } else {   
  49.                         c.mat[i][j] = min(c.mat[i][j], v);
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.  
  55.         return c;
  56.     }
  57.  
  58.     friend void operator *= (Matrix &a, Matrix b) {
  59.         a = a * b;
  60.     }
  61.  
  62.     void operator = (Matrix b) {
  63.         mat = b.mat;
  64.     }
  65. } g;
  66.  
  67. int n;
  68.  
  69. bool vis[N];
  70.  
  71. void dfs(int v) {
  72.     for (int i = 0; i < n; i++) {
  73.         if (!vis[i] && g.mat[v][i] != inf) {
  74.             vis[i] = true;
  75.             dfs(i);
  76.         }
  77.     }
  78. }
  79.  
  80. Matrix bin_pow(Matrix a, int n) {
  81.     Matrix r = a;
  82.     --n;
  83.     while (n > 0) {
  84.         if (n & 1) {
  85.             if (r.mat.empty()) {
  86.                 r = a;
  87.             } else {
  88.                 r *= a;
  89.             }
  90.         }
  91.         a *= a;
  92.         n >>= 1;
  93.     }
  94.     return r;
  95. }
  96.  
  97. int main() {
  98.     int m, s, t;
  99.     cin >> n >> m >> s >> t;
  100.     --s;
  101.     --t;
  102.    
  103.     g = Matrix(n);
  104.  
  105.     for (int i = 0; i < m; i++) {
  106.         int x, y, l;
  107.         cin >> x >> y >> l;
  108.         --x;
  109.         --y;
  110.         g.mat[x][y] = min(g.mat[x][y], l);
  111.         g.mat[y][x] = min(g.mat[y][x], l);
  112.     }
  113.  
  114.     dfs(s);
  115.  
  116.     if (vis[s] && vis[t]) {
  117.         Matrix res = bin_pow(g, 2018);
  118.  
  119.         while (true) {
  120.             if (res.mat[s][t] != inf) {
  121.                 cout << res.mat[s][t] << "\n";
  122.                 break;
  123.             } else {
  124.                 res *= res;
  125.                 res *= g;
  126.             }
  127.         }
  128.     } else {
  129.         puts("Mumkun emes");
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement