This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
int edge[n][n], dis[n][n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
edge[i][j] = inf;
for(int l = 0; l < n; l++){
dis[i][j][l] = 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;
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][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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |