#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;
int n, m, k, q, adj[70][70], dp[70][70][70];
int main(){
cin >> n >> m;
memset(adj, INT_MAX, sizeof(adj));
for(int i = 0; i < n; i++) adj[i][i] = 0;
while(m--){
int u, v, w; cin >> u >> v >> w;
adj[u - 1][v - 1] = min(adj[u - 1][v - 1], w);
}
cin >> k >> q;
k = (k > n - 1) ? n - 1 : k;
memset(dp, 0x3f, sizeof(dp));
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j) {
dp[0][i][j]=i==j?0:INF;
dp[1][i][j] = adj[i][j];
}
for(int i = 2; i < k + 1; i++) for(int u = 0; u < n; u++) for(int u2 = 0; u2 < n; u2++) for(int v = 0; v < n; v++) dp[i][u][v] = min(dp[i][u][v], dp[i - 1][u][u2] + adj[u2][v]);
while(q--) {
int a, b;
cin >> a >> b, --a, --b;
cout << (dp[k][a][b]>=INF?-1:dp[k][a][b]) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
1620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |