#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
using namespace std;
const int mod = 998244353;
const int N = 2e5 + 5;
const int inf = 1e15 + 5;
int32_t main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
int n, m;
cin>>n>>m;
int edge[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
edge[i][j] = inf;
}
}
for(int i = 0; i < m; i++){
int a, b, c;
cin>>a>>b>>c;
a--, b--;
edge[a][b] = min(edge[a][b], c);
}
int mndis[n][n];
int k, q;
cin>>k>>q;
k = min(k, n);
int dis[n][n][k+1];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int l = 0; l <= k; l++){
dis[i][j][l] = inf;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
dis[i][j][1] = edge[i][j];
}
for(int len = 0; len <= k; len++){
dis[i][i][len] = 0;
}
}
for(int len = 2; len <= k; len++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int kk = 0; kk < n; kk++){
for(int lenf = 0; lenf <= len; lenf++){
dis[i][j][len] = min(dis[i][j][len], dis[i][kk][lenf] + dis[kk][j][len - lenf]);
}
}
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int ans = inf;
for(int len = 0; len <= k; len++){
ans = min(ans, dis[i][j][len]);
}
mndis[i][j] = ans;
}
}
while(q--){
int a, b;
cin>>a>>b;
a--, b--;
if(mndis[a][b] < inf) cout<<mndis[a][b]<<endl;
else cout<<-1<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
404 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
604 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
9 ms |
604 KB |
Output is correct |
4 |
Correct |
7 ms |
600 KB |
Output is correct |
5 |
Correct |
9 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
404 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
153 ms |
1732 KB |
Output is correct |
8 |
Correct |
967 ms |
3168 KB |
Output is correct |
9 |
Correct |
10 ms |
604 KB |
Output is correct |
10 |
Correct |
329 ms |
1884 KB |
Output is correct |
11 |
Correct |
31 ms |
856 KB |
Output is correct |
12 |
Correct |
458 ms |
2392 KB |
Output is correct |
13 |
Correct |
81 ms |
6740 KB |
Output is correct |
14 |
Correct |
93 ms |
6848 KB |
Output is correct |
15 |
Correct |
764 ms |
8644 KB |
Output is correct |
16 |
Correct |
201 ms |
7252 KB |
Output is correct |
17 |
Correct |
81 ms |
6744 KB |
Output is correct |
18 |
Correct |
77 ms |
6720 KB |
Output is correct |
19 |
Execution timed out |
1045 ms |
9396 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
404 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
604 KB |
Output is correct |
9 |
Correct |
9 ms |
604 KB |
Output is correct |
10 |
Correct |
7 ms |
600 KB |
Output is correct |
11 |
Correct |
9 ms |
604 KB |
Output is correct |
12 |
Correct |
8 ms |
604 KB |
Output is correct |
13 |
Correct |
9 ms |
604 KB |
Output is correct |
14 |
Correct |
153 ms |
1732 KB |
Output is correct |
15 |
Correct |
967 ms |
3168 KB |
Output is correct |
16 |
Correct |
10 ms |
604 KB |
Output is correct |
17 |
Correct |
329 ms |
1884 KB |
Output is correct |
18 |
Correct |
31 ms |
856 KB |
Output is correct |
19 |
Correct |
458 ms |
2392 KB |
Output is correct |
20 |
Correct |
81 ms |
6740 KB |
Output is correct |
21 |
Correct |
93 ms |
6848 KB |
Output is correct |
22 |
Correct |
764 ms |
8644 KB |
Output is correct |
23 |
Correct |
201 ms |
7252 KB |
Output is correct |
24 |
Correct |
81 ms |
6744 KB |
Output is correct |
25 |
Correct |
77 ms |
6720 KB |
Output is correct |
26 |
Execution timed out |
1045 ms |
9396 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |