This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
const int INF = 1e18;
int N, M, K, Q, dist[80][80], dp[80][80][80];
void chmin(int& x, int y) {
x = min(x, y);
}
signed main() {
fast;
cin >> N >> M;
for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) dist[i][j] = (i == j ? 0 : INF);
for (int i = 1; i <= M; ++i) {
int u, v, c; cin >> u >> v >> c;
dist[u][v] = min(dist[u][v], c);
}
cin >> K >> Q;
for (int k = 1; k <= min(N, K); ++k) {
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
dp[i][j][k] = INF;
if (k == 1) {
if (dist[i][j] != INF) chmin(dp[i][j][k], dist[i][j]);
} else {
for (int mid = 1; mid <= N; ++mid) {
if (dist[mid][j] != INF) chmin(dp[i][j][k], dp[i][mid][k - 1] + dist[mid][j]);
}
}
}
}
}
while (Q--) {
int u, v; cin >> u >> v;
int ans = INF;
for (int i = 1; i <= min(N, K); ++i) chmin(ans, dp[u][v][i]);
cout << (ans == INF ? -1 : ans) << '\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... |