#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e17*2;
const int N = 3e5 + 5;
int32_t main(){
int n, m;
cin>>n>>m;
vector<vector<int> > edge(n, vector<int>(n, inf));
for(int i = 0; i < m; i++){
int u, v, w;
cin>>u>>v>>w;
edge[u-1][v-1] = min(edge[u-1][v-1], w);
}
int k, q;
cin>>k>>q;
int dis[n][n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int l = 0; l < n; 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];
}
dis[i][i][1] = 0;
}
for(int asama = 2; asama < n; asama++){
for(int l = 0; l < n; l++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
dis[i][j][asama] = min(dis[i][j][asama], dis[i][l][asama-1] + dis[l][j][asama-1]);
}
}
}
}
while(q--){
int c, d;
cin>>c>>d;
c--, d--;
if(dis[c][d][min(k, n-1)] == inf) cout<<-1<<endl;
else cout<<dis[c][d][min(k, n-1)]<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
544 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
2908 KB |
Output is correct |
2 |
Correct |
46 ms |
3156 KB |
Output is correct |
3 |
Incorrect |
46 ms |
3144 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
544 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
544 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |