-
Notifications
You must be signed in to change notification settings - Fork 0
/
Грузоперевозки.cpp
60 lines (50 loc) · 1.41 KB
/
Грузоперевозки.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <iostream>
#include <vector>
#define INTMAX 2147483647
using namespace std;
void FloydWarshall(vector<vector<int>> &graph, int n){
for (int k = 0; k < n; ++k){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
if (graph[i][k] < INTMAX && graph[k][j] < INTMAX)
graph[i][j] = min(graph[i][j],graph[i][k] + graph[k][j]);
}
}
}
}
int main() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n, m, q, s1, s2, w;
cin >> n >> m;
vector<vector<int>> graph;
graph.resize(n);
for (int i = 0; i < n; i++){
graph[i].resize(n);
for (int j = 0; j < n; j++)
graph[i][j] = INTMAX;
graph[i][i] = 0;
}
for (int i = 0; i < m; i++){
cin >> s1 >> s2 >> w;
graph[--s1][--s2] = w;
}
cin >> q;
FloydWarshall(graph, n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if ((graph[i][j] != INTMAX) && (graph[j][i] != INTMAX)){
graph[i][j] = 0;
graph[j][i] = 0;
}
FloydWarshall(graph, n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (graph[i][j] == INTMAX)
graph[i][j] = -1;
for (int i = 0; i < q; ++i){
cin >> s1 >> s2;
cout << graph[--s1][--s2] << '\n';
}
}