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>
using namespace std;
#define ll long long
#define ar array
const int INF=1e9;
int n, m, k, q, d[70][70], dp[70][70][70];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j)
d[i][j]=i==j?0:INF;
for (int i=0; i<m; ++i) {
int u, v, t;
cin >> u >> v >> t, --u, --v;
d[u][v]=min(d[u][v], t);
}
cin >> k >> q;
k=min(k, n-1);
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]=d[i][j];
}
for (int i=2; i<=k; ++i)
for (int a=0; a<n; ++a)
for (int b=0; b<n; ++b)
for (int c=0; c<n; ++c)
dp[i][a][c]=min(dp[i][a][c], dp[i-1][a][b]+d[b][c]);
while(q--) {
int a, b;
cin >> a >> b, --a, --b;
cout << (dp[k][a][b]>=INF?-1:dp[k][a][b]) << "\n";
}
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... |