forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flloyd-WarshallAlgo.cpp
51 lines (45 loc) ยท 1.1 KB
/
Flloyd-WarshallAlgo.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
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long int ll;
int main(){
// write your code here
ll t;
cin>>t;
while(t--){
ll n,e;
cin>>n>>e;
vector<vector<ll>> dp(n, vector<ll>(n, 1e9));
for(ll i=0 ; i<e ; i++){
ll a,b,w;
cin>>a>>b>>w;
if(dp[a-1][b-1]>w){
dp[a-1][b-1] = w;
}
if(dp[b-1][a-1]>w){
dp[b-1][a-1] = w;
}
}
for(ll i=0 ; i<n ; i++)
dp[i][i] = 0;
for(ll k=0 ; k<n ; k++){
for(ll i=0 ; i<n ; i++){
for(ll j=0 ; j<n ; j++){
if(dp[i][k]!=1e9 && dp[k][j]!=1e9 && dp[i][j]>(dp[i][k]+dp[k][j])){
//cout<<i<<" "<<j<<"\n";
dp[i][j] = dp[i][k]+dp[k][j];
}
}
}
}
ll q;
cin>>q;
while(q--){
ll x,y;
cin>>x>>y;
cout<<dp[x-1][y-1]<<"\n";
}
}
return 0;
}