Naxocist

1221_Market

May 15th, 2022 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. using ll = long long;
  6. using pi = pair<int, int>;
  7. using tiii = tuple<int, int, int>;
  8.  
  9.  
  10. const int N = 505, mod = 1e9 + 7;
  11. int ar[N], dist[N][N], node[N][N];
  12.  
  13.  
  14. int main() {
  15.     // freopen("input.txt", "r", stdin);
  16.     int n, m, q; scanf("%d%d%d", &n, &m, &q);
  17.  
  18.     for(int i=1; i<=n; ++i){
  19.         for(int j=1; j<=n; ++j){
  20.             dist[i][j] = 2e9;
  21.         }
  22.     }
  23.  
  24.     for(int i=1; i<=n; ++i) scanf("%d", &ar[i]), dist[i][i] = 0;
  25.  
  26.     for(int i=1; i<=n; ++i){
  27.         for(int j=1; j<=n; ++j){
  28.             dist[i][j] = ar[i] + ar[j];
  29.             node[i][j] = 2;
  30.         }
  31.     }
  32.  
  33.     for(int i=0; i<m; ++i){
  34.         int u, v, w; scanf("%d%d%d", &u, &v, &w);
  35.         dist[u][v] = dist[v][u] = w;
  36.     }
  37.  
  38.  
  39.     for(int k=1 ; k<=n; ++k){
  40.         for(int i=1; i<=n; ++i){
  41.             for(int j=1; j<=n; ++j){
  42.                 if(dist[i][k] + dist[k][j] < dist[i][j]){
  43.                     dist[i][j] = dist[i][k] + dist[k][j];
  44.                     node[i][j] = node[i][k] + node[k][j];
  45.                 }else if(dist[i][k] + dist[k][j] == dist[i][j]){
  46.                     if(node[i][k] + node[k][j] > node[i][j]) {
  47.                         node[i][j] = node[i][k] + node[k][j];
  48.                     }
  49.  
  50.                 }
  51.             }
  52.         }
  53.     }
  54.  
  55.    
  56.     while(q--){
  57.         int s, e; scanf("%d%d", &s, &e);
  58.  
  59.         if(dist[s][e] <= ar[s] + ar[e]){
  60.             printf("%d %d\n", dist[s][e], node[s][e]);
  61.         }else{
  62.             printf("%d %d\n", ar[s] + ar[e], 2);
  63.         }
  64.  
  65.     }
  66.     return 0;
  67. }
  68.  
Add Comment
Please, Sign In to add comment