File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ // link : https://cses.fi/problemset/task/1672/
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+ #define int long long
5
+ #define INF (int ) (1e18 )
6
+ const int N=1e5 +10 ;
7
+ signed main () {
8
+ int n,m,q;
9
+ cin>>n>>m>>q;
10
+ int arr[n+1 ][n+1 ];
11
+ for (int i=1 ;i<=n;i++){
12
+ for (int j=1 ;j<=n;j++){
13
+ arr[i][j]=INF;
14
+ if (i==j)arr[i][j]=0 ;
15
+ }
16
+ }
17
+ for (int i=0 ;i<m;i++){
18
+ int a,b,w;
19
+ cin>>a>>b>>w;
20
+ arr[a][b]=min (arr[a][b],w);
21
+ arr[b][a]=min (arr[b][a],w);
22
+ }
23
+ for (int k=1 ;k<=n;k++){
24
+ for (int i=1 ;i<=n;i++){
25
+ for (int j=1 ;j<=n;j++){
26
+ arr[i][j]=min (arr[i][j],arr[i][k]+arr[k][j]);
27
+ }
28
+ }
29
+ }
30
+ while (q--){
31
+ int t,r;
32
+ cin>>t>>r;
33
+ if (arr[t][r]==INF)cout<<-1 <<endl;
34
+ else cout<<arr[t][r]<<endl;
35
+ }
36
+ return 0 ;
37
+ }
You can’t perform that action at this time.
0 commit comments