#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
inline void solve() {
int n, m;
cin >> n >> m;
ve<ve<int>> c(n,ve<int>(n,iinf));
for (int i = 0; i < m; ++i) {
int v, u, w;
cin >> v >> u >> w;
chmin(c[--v][--u], w);
}
ve<ve<pii>> gr(n);
for (int v = 0; v < n; ++v) {
for (int u = 0; u < n; ++u) {
if (c[v][u] == iinf) continue;
gr[v].pb({u, c[v][u]});
}
}
ve<ve<ve<ll>>>dist(n,ve<ve<ll>>(n,ve<ll>(n+1,inf)));
for (int s = 0; s < n; ++s) {
auto &d = dist[s];
d[s][0] = 0;
set<pair<int,pii>> st;
st.insert({0, {s, 0}});
while (sz(st)) {
auto [v, c] = st.begin()->se;
st.erase(st.begin());
if (c == n) continue;
for (auto &[to, w] : gr[v]) {
if (d[to][c + 1] > d[v][c] + w) {
st.erase({d[to][c + 1], {to, c + 1}});
d[to][c + 1] = d[v][c] + w;
st.insert({d[to][c + 1], {to, c + 1}});
}
}
}
}
int k, q;
cin >> k >> q;
chmin(k, n);
for (int s = 0; s < n; ++s) {
for (int to = 0; to < n; ++to) {
for (int t = 1; t <= n; ++t) {
chmin(dist[s][to][t], dist[s][to][t - 1]);
}
}
}
while (q--) {
int v, u;
cin >> v >> u;
ll ans = dist[--v][--u][k];
if (ans == inf) ans = -1;
cout << ans << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
3280 KB |
Output is correct |
2 |
Correct |
142 ms |
3276 KB |
Output is correct |
3 |
Correct |
142 ms |
3276 KB |
Output is correct |
4 |
Correct |
158 ms |
3264 KB |
Output is correct |
5 |
Correct |
161 ms |
3284 KB |
Output is correct |
6 |
Correct |
298 ms |
3284 KB |
Output is correct |
7 |
Correct |
304 ms |
3388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
144 ms |
3364 KB |
Output is correct |
8 |
Correct |
142 ms |
3364 KB |
Output is correct |
9 |
Correct |
191 ms |
3260 KB |
Output is correct |
10 |
Correct |
169 ms |
3300 KB |
Output is correct |
11 |
Correct |
268 ms |
3364 KB |
Output is correct |
12 |
Correct |
286 ms |
3372 KB |
Output is correct |
13 |
Correct |
381 ms |
9476 KB |
Output is correct |
14 |
Correct |
422 ms |
9476 KB |
Output is correct |
15 |
Correct |
390 ms |
9420 KB |
Output is correct |
16 |
Correct |
388 ms |
9532 KB |
Output is correct |
17 |
Correct |
296 ms |
9548 KB |
Output is correct |
18 |
Correct |
310 ms |
9552 KB |
Output is correct |
19 |
Correct |
293 ms |
9528 KB |
Output is correct |
20 |
Correct |
295 ms |
9536 KB |
Output is correct |
21 |
Correct |
369 ms |
9580 KB |
Output is correct |
22 |
Correct |
380 ms |
9676 KB |
Output is correct |
23 |
Correct |
363 ms |
9600 KB |
Output is correct |
24 |
Correct |
362 ms |
9536 KB |
Output is correct |
25 |
Correct |
394 ms |
9792 KB |
Output is correct |
26 |
Correct |
380 ms |
9676 KB |
Output is correct |
27 |
Correct |
395 ms |
9672 KB |
Output is correct |
28 |
Correct |
396 ms |
9676 KB |
Output is correct |
29 |
Correct |
531 ms |
9684 KB |
Output is correct |
30 |
Correct |
536 ms |
9680 KB |
Output is correct |
31 |
Correct |
531 ms |
9784 KB |
Output is correct |
32 |
Correct |
535 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
143 ms |
3280 KB |
Output is correct |
8 |
Correct |
142 ms |
3276 KB |
Output is correct |
9 |
Correct |
142 ms |
3276 KB |
Output is correct |
10 |
Correct |
158 ms |
3264 KB |
Output is correct |
11 |
Correct |
161 ms |
3284 KB |
Output is correct |
12 |
Correct |
298 ms |
3284 KB |
Output is correct |
13 |
Correct |
304 ms |
3388 KB |
Output is correct |
14 |
Correct |
144 ms |
3364 KB |
Output is correct |
15 |
Correct |
142 ms |
3364 KB |
Output is correct |
16 |
Correct |
191 ms |
3260 KB |
Output is correct |
17 |
Correct |
169 ms |
3300 KB |
Output is correct |
18 |
Correct |
268 ms |
3364 KB |
Output is correct |
19 |
Correct |
286 ms |
3372 KB |
Output is correct |
20 |
Correct |
381 ms |
9476 KB |
Output is correct |
21 |
Correct |
422 ms |
9476 KB |
Output is correct |
22 |
Correct |
390 ms |
9420 KB |
Output is correct |
23 |
Correct |
388 ms |
9532 KB |
Output is correct |
24 |
Correct |
296 ms |
9548 KB |
Output is correct |
25 |
Correct |
310 ms |
9552 KB |
Output is correct |
26 |
Correct |
293 ms |
9528 KB |
Output is correct |
27 |
Correct |
295 ms |
9536 KB |
Output is correct |
28 |
Correct |
369 ms |
9580 KB |
Output is correct |
29 |
Correct |
380 ms |
9676 KB |
Output is correct |
30 |
Correct |
363 ms |
9600 KB |
Output is correct |
31 |
Correct |
362 ms |
9536 KB |
Output is correct |
32 |
Correct |
394 ms |
9792 KB |
Output is correct |
33 |
Correct |
380 ms |
9676 KB |
Output is correct |
34 |
Correct |
395 ms |
9672 KB |
Output is correct |
35 |
Correct |
396 ms |
9676 KB |
Output is correct |
36 |
Correct |
531 ms |
9684 KB |
Output is correct |
37 |
Correct |
536 ms |
9680 KB |
Output is correct |
38 |
Correct |
531 ms |
9784 KB |
Output is correct |
39 |
Correct |
535 ms |
9676 KB |
Output is correct |
40 |
Correct |
125 ms |
3396 KB |
Output is correct |
41 |
Correct |
130 ms |
3308 KB |
Output is correct |
42 |
Correct |
194 ms |
3404 KB |
Output is correct |
43 |
Correct |
179 ms |
3404 KB |
Output is correct |
44 |
Correct |
291 ms |
3364 KB |
Output is correct |
45 |
Correct |
291 ms |
3368 KB |
Output is correct |
46 |
Correct |
400 ms |
9536 KB |
Output is correct |
47 |
Correct |
400 ms |
9536 KB |
Output is correct |
48 |
Correct |
396 ms |
9420 KB |
Output is correct |
49 |
Correct |
405 ms |
9408 KB |
Output is correct |
50 |
Correct |
295 ms |
9548 KB |
Output is correct |
51 |
Correct |
291 ms |
9676 KB |
Output is correct |
52 |
Correct |
302 ms |
9528 KB |
Output is correct |
53 |
Correct |
297 ms |
9544 KB |
Output is correct |
54 |
Correct |
395 ms |
9584 KB |
Output is correct |
55 |
Correct |
394 ms |
9576 KB |
Output is correct |
56 |
Correct |
391 ms |
9524 KB |
Output is correct |
57 |
Correct |
397 ms |
9676 KB |
Output is correct |
58 |
Correct |
418 ms |
9668 KB |
Output is correct |
59 |
Correct |
417 ms |
9668 KB |
Output is correct |
60 |
Correct |
409 ms |
9688 KB |
Output is correct |
61 |
Correct |
414 ms |
9676 KB |
Output is correct |
62 |
Correct |
540 ms |
9800 KB |
Output is correct |
63 |
Correct |
549 ms |
9668 KB |
Output is correct |
64 |
Correct |
532 ms |
9664 KB |
Output is correct |
65 |
Correct |
541 ms |
9796 KB |
Output is correct |