DuongNhi99

CJKHAOSAT (lqdoj) - Floyd

Mar 30th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. using pi32 = pair<int, int>;
  6. using pi64 = pair<int64_t, int64_t>;
  7. using ti32 = tuple<int, int, int>;
  8. using ti64 = tuple<int64_t, int64_t, int64_t>;
  9.  
  10. const int N = 400 + 5;
  11. const int INF = 1e9 + 7;
  12.  
  13. int n, nE, nQ;
  14. int graph[N][N];
  15.  
  16. int64_t d[N][N];
  17.  
  18. void Floyd() {
  19.     for (int i = 1; i <= n; ++i)
  20.         for (int j = 1; j <= n; ++j)
  21.             if (i != j)
  22.                 d[i][j] = graph[i][j];
  23.  
  24.  
  25.     for (int k = 1; k <= n; ++k)
  26.         for (int i = 1; i <= n; ++i)
  27.             for (int j = 1; j <= n; ++j)
  28.                 d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  29. }
  30.  
  31. int main() {
  32. #ifdef LOCAL
  33.     freopen("in.txt", "r", stdin);
  34. #else
  35.     freopen("CJKHAOSAT.inp", "r", stdin);
  36.     freopen("CJKHAOSAT.out", "w", stdout);
  37. #endif
  38.     ios_base::sync_with_stdio(false);
  39.     cin.tie(nullptr);
  40.  
  41.     cin >> n >> nE >> nQ;
  42.  
  43.     fill_n(*graph, N * N, INF);
  44.     for (int i = 1; i <= nE; i++) {
  45.         int u, v, w; cin >> u >> v >> w;
  46.         graph[u][v] = graph[v][u] = min(graph[u][v], w);
  47.     }
  48.  
  49.     Floyd();
  50.  
  51.     while (nQ--) {
  52.         int u, v; cin >> u >> v;
  53.         cout << d[u][v] << '\n';
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment