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"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int N = 75;
const int INF = 1e9;
int d[N][N];
int cnt[N][N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#ifdef JASPER
freopen("in2", "r", stdin);
#endif
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
d[i][j] = cnt[i][j] = INF;
if (i == j) d[i][j] = cnt[i][j] = 0;
}
}
vector <array <int, 3>> edges;
for (int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
edges.push_back({u, v, w});
}
int K, q;
cin >> K >> q;
for (int i = 0; i < m; ++i) {
auto [u, v, w] = edges[i];
if (w < d[u][v]) {
d[u][v] = w;
cnt[u][v] = 1;
}
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (d[i][k] + d[k][j] < d[i][j] && cnt[i][k] + cnt[k][j] <= K) {
d[i][j] = d[i][k] + d[k][j];
cnt[i][j] = cnt[i][k] + cnt[k][j];
}
else if (d[i][k] + d[k][j] == d[i][j] && cnt[i][k] + cnt[k][j] <= K) {
cnt[i][j] = min(cnt[i][j], cnt[i][k] + cnt[k][j]);
}
}
}
}
// cout << d[1][4] << "\n";
for (int _q = 1; _q <= q; ++_q) {
int x, y;
cin >> x >> y;
if (d[x][y] == INF || cnt[x][y] == INF) {
cout << "-1\n";
}
else
cout << d[x][y] << "\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... |