-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathD_Shortest_Routes_II.cpp
More file actions
82 lines (66 loc) · 1.68 KB
/
D_Shortest_Routes_II.cpp
File metadata and controls
82 lines (66 loc) · 1.68 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <numeric>
#include <climits>
#define int long long
using namespace std;
int n , m , q;
vector<vector<int>> dist;
void solve() {
cin >> n >> m >> q;
dist.assign(n+1 , vector<int>(n+1 , LLONG_MAX));
for(int i=1 ; i<=n ;i++) dist[i][i] = 0;
for(int i=0 ;i<m ; i++){
int a , b , c;
cin >> a >> b >> c;
dist[a][b] = min(dist[a][b] , c);
dist[b][a] = min(dist[b][a] , c);
}
//implement floys-warshall
for(int k=1 ; k<=n ; k++){
for(int i=1 ; i<=n ; i++){
for(int j=1 ; j<=n ; j++){
if (dist[i][k] != LLONG_MAX && dist[k][j] != LLONG_MAX) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
for(int i=0 ;i<q ; i++){
int s , d;
cin >> s >> d;
if(dist[s][d] == LLONG_MAX) cout << -1 << endl;
else cout << dist[s][d] << endl;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int _t = 1;
// cin >> _t;
while(_t--){
solve();
}
return 0;
}
/*
floyd-warshall algorithm -> use to find shortest dist b/w all pair of nodes in n^3 time complexity
for(int i=1 ; i<=n ;i++) dist[i][i] = 0; --> initialize diagonal with zero
llong_max and overflow check
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
*/