Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <limits.h>
- #include <algorithm>
- using namespace std;
- int mat[410][410];
- int main() {
- int n,m;
- cin >> n >> m;
- int x,y,r;
- int i;
- for(i = 1; i <= n; i++) {
- for(int j = 1; j <= n; j++) {
- if(i == j)
- mat[i][j] = 0;
- else
- mat[i][j] = INT_MAX;
- }
- }
- for(i = 0; i < m; i++) {
- cin >> x >> y >> r;
- mat[x][y] = r;
- }
- for(int k = 1; k <= n; k++) {
- for(int i = 1; i <= n; i++) {
- for(int j = 1; j <= n; j++) {
- if(mat[i][k] != INT_MAX && mat[k][j] != INT_MAX)
- mat[i][j] = min(mat[i][j],mat[i][k]+mat[k][j]);
- }
- }
- }
- int q;
- cin >> q;
- int l;
- for(i = 0; i < q; i++) {
- cin >> l >> r;
- if(mat[l][r] != INT_MAX) {
- cout << mat[l][r] << '\n';
- }
- else {
- cout << -1 << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement