#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
const int mod = 1e9 + 7;
const int N = 3e5 + 5;
int main(){
int n, m;
cin>>n>>m;
vector<vector<int> > edge(n, vector<int>(n, -1));
for(int i = 0; i < m; i++){
int u, v, w;
cin>>u>>v>>w;
if(edge[u-1][v-1] == -1) edge[u-1][v-1] = w;
else 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 = 1; l < n; l++){
dis[i][j][l] = mod;
}
dis[i][j][0] = 0;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(edge[i][j] != -1) dis[i][j][1] = edge[i][j];
if(i == j) dis[i][j][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)] == mod) 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
1820 KB |
Output is correct |
2 |
Correct |
45 ms |
1824 KB |
Output is correct |
3 |
Incorrect |
45 ms |
1796 KB |
Output isn't correct |
4 |
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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |