Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- vector<vector<int>> graph;
- int n, m;
- void FW() {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- for (int h = 0; h < n; h++) {
- if ((graph[j][i] != INT_MAX) && (graph[i][h] != INT_MAX)) {
- graph[j][h] = min(graph[j][h], graph[j][i] + graph[i][h]);
- }
- }
- }
- }
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- cin >> n >> m;
- graph.resize(n, vector<int>(n, INT_MAX));
- for (int i = 0; i < n; i++) {
- graph[i][i] = 0;
- }
- int a, b;
- for (int i = 0; i < m; i++) {
- cin >> a >> b;
- cin >> graph[a - 1][b - 1];
- }
- FW();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if ((graph[i][j]!= INT_MAX) && (graph[j][i] != INT_MAX)) {
- graph[i][j] = 0;
- graph[j][i] = 0;
- }
- }
- }
- FW();
- int k;
- cin >> k;
- for (int i = 0; i < k; i++) {
- cin >> a >> b;
- if (graph[a - 1][b - 1] != INT_MAX) {
- cout << graph[a - 1][b - 1] << endl;
- }
- else{
- cout << -1 << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement