#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
vector<vector<ll> > dis;
int n,m;
vector<vector<ll> > ans;
void pow(int p) {
ans.resize(n);
for(int i=0; i<n; i++) ans[i].resize(n,1e17);
for(int i=0;i<n;i++) ans[i][i]=0;
vector<vector<ll> > og;
while(p) {
if(p&1) {
og = ans;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
for(int k=0; k<n; k++) {
og[i][j] = min(og[i][j],ans[i][k]+dis[k][j]);
}
}
}
ans = og;
}
og = dis;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
for(int k=0; k<n; k++) {
og[i][j] = min(og[i][j],dis[i][k]+dis[k][j]);
}
}
}
dis = og;
p>>=1;
}
}
int main() {
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
dis.resize(n);
for(int i=0; i<n; i++) dis[i].resize(n,1e17);
for(int i=0; i<n; i++) dis[i][i]=0;
for(int i=0; i<m; i++) {
ll a,b,c;
cin>>a>>b>>c;
a--;b--;
dis[a][b] = min(dis[a][b],c);
}
int k,q;cin>>k>>q;
pow(k);
while(q--){
int a,b;cin>>a>>b;
a--;b--;
if(ans[a][b]>5e16){
cout<<-1<<"\n";
}else{
cout<<ans[a][b]<<"\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
8 |
Correct |
5 ms |
500 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
5 ms |
456 KB |
Output is correct |
11 |
Correct |
5 ms |
468 KB |
Output is correct |
12 |
Correct |
6 ms |
468 KB |
Output is correct |
13 |
Correct |
96 ms |
6484 KB |
Output is correct |
14 |
Correct |
88 ms |
6480 KB |
Output is correct |
15 |
Correct |
92 ms |
6632 KB |
Output is correct |
16 |
Correct |
88 ms |
6480 KB |
Output is correct |
17 |
Correct |
86 ms |
6600 KB |
Output is correct |
18 |
Correct |
90 ms |
6604 KB |
Output is correct |
19 |
Correct |
87 ms |
6604 KB |
Output is correct |
20 |
Correct |
91 ms |
6740 KB |
Output is correct |
21 |
Correct |
91 ms |
6608 KB |
Output is correct |
22 |
Correct |
89 ms |
6600 KB |
Output is correct |
23 |
Correct |
86 ms |
6604 KB |
Output is correct |
24 |
Correct |
88 ms |
6608 KB |
Output is correct |
25 |
Correct |
88 ms |
6744 KB |
Output is correct |
26 |
Correct |
89 ms |
6728 KB |
Output is correct |
27 |
Correct |
95 ms |
6728 KB |
Output is correct |
28 |
Correct |
93 ms |
6728 KB |
Output is correct |
29 |
Correct |
87 ms |
6608 KB |
Output is correct |
30 |
Correct |
90 ms |
6600 KB |
Output is correct |
31 |
Correct |
91 ms |
6480 KB |
Output is correct |
32 |
Correct |
91 ms |
6484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
500 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
5 ms |
456 KB |
Output is correct |
18 |
Correct |
5 ms |
468 KB |
Output is correct |
19 |
Correct |
6 ms |
468 KB |
Output is correct |
20 |
Correct |
96 ms |
6484 KB |
Output is correct |
21 |
Correct |
88 ms |
6480 KB |
Output is correct |
22 |
Correct |
92 ms |
6632 KB |
Output is correct |
23 |
Correct |
88 ms |
6480 KB |
Output is correct |
24 |
Correct |
86 ms |
6600 KB |
Output is correct |
25 |
Correct |
90 ms |
6604 KB |
Output is correct |
26 |
Correct |
87 ms |
6604 KB |
Output is correct |
27 |
Correct |
91 ms |
6740 KB |
Output is correct |
28 |
Correct |
91 ms |
6608 KB |
Output is correct |
29 |
Correct |
89 ms |
6600 KB |
Output is correct |
30 |
Correct |
86 ms |
6604 KB |
Output is correct |
31 |
Correct |
88 ms |
6608 KB |
Output is correct |
32 |
Correct |
88 ms |
6744 KB |
Output is correct |
33 |
Correct |
89 ms |
6728 KB |
Output is correct |
34 |
Correct |
95 ms |
6728 KB |
Output is correct |
35 |
Correct |
93 ms |
6728 KB |
Output is correct |
36 |
Correct |
87 ms |
6608 KB |
Output is correct |
37 |
Correct |
90 ms |
6600 KB |
Output is correct |
38 |
Correct |
91 ms |
6480 KB |
Output is correct |
39 |
Correct |
91 ms |
6484 KB |
Output is correct |
40 |
Correct |
5 ms |
468 KB |
Output is correct |
41 |
Correct |
18 ms |
460 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
16 ms |
480 KB |
Output is correct |
44 |
Correct |
4 ms |
456 KB |
Output is correct |
45 |
Correct |
19 ms |
440 KB |
Output is correct |
46 |
Correct |
87 ms |
6480 KB |
Output is correct |
47 |
Correct |
87 ms |
6472 KB |
Output is correct |
48 |
Correct |
92 ms |
6484 KB |
Output is correct |
49 |
Correct |
104 ms |
6480 KB |
Output is correct |
50 |
Correct |
88 ms |
6608 KB |
Output is correct |
51 |
Correct |
87 ms |
6600 KB |
Output is correct |
52 |
Correct |
86 ms |
6600 KB |
Output is correct |
53 |
Correct |
102 ms |
6600 KB |
Output is correct |
54 |
Correct |
89 ms |
6604 KB |
Output is correct |
55 |
Correct |
90 ms |
6608 KB |
Output is correct |
56 |
Correct |
89 ms |
6604 KB |
Output is correct |
57 |
Correct |
104 ms |
6732 KB |
Output is correct |
58 |
Correct |
97 ms |
6744 KB |
Output is correct |
59 |
Correct |
103 ms |
6940 KB |
Output is correct |
60 |
Correct |
93 ms |
6728 KB |
Output is correct |
61 |
Correct |
106 ms |
6740 KB |
Output is correct |
62 |
Correct |
85 ms |
6428 KB |
Output is correct |
63 |
Correct |
93 ms |
6484 KB |
Output is correct |
64 |
Correct |
94 ms |
6468 KB |
Output is correct |
65 |
Correct |
100 ms |
6468 KB |
Output is correct |