Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <limits.h>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int mat[410][410];
  10. int main() {
  11.     int n,m;
  12.     cin >> n >> m;
  13.     int x,y,r;
  14.     int i;
  15.     for(i = 1; i <= n; i++) {
  16.         for(int j = 1; j <= n; j++) {
  17.             if(i == j)
  18.                 mat[i][j] = 0;
  19.             else
  20.                 mat[i][j] = INT_MAX;
  21.         }
  22.     }
  23.     for(i = 0; i < m; i++) {
  24.         cin >> x >> y >> r;
  25.         mat[x][y] = r;
  26.     }
  27.  
  28.     for(int k = 1; k <= n; k++) {
  29.         for(int i = 1; i <= n; i++) {
  30.             for(int j = 1; j <= n; j++) {
  31.                 if(mat[i][k] != INT_MAX && mat[k][j] != INT_MAX)
  32.                     mat[i][j] = min(mat[i][j],mat[i][k]+mat[k][j]);
  33.             }
  34.         }
  35.     }
  36.     int q;
  37.     cin >> q;
  38.     int l;
  39.     for(i = 0; i < q; i++) {
  40.         cin >> l >> r;
  41.         if(mat[l][r] != INT_MAX) {
  42.             cout << mat[l][r] << '\n';
  43.         }
  44.         else {
  45.             cout << -1 << '\n';
  46.         }
  47.     }
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement