# pragma GCC optimize("O3,unroll-loops")
# pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
# include <bits/stdc++.h>
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
# define mkp make_pair
# define ff first
# define ss second
# define pll pair <ll, ll>
# define pii pair <int, int>
# define vec vector
# define pb push_back
# define pf push_front
# define ppb pop_back
# define ppf pop_front
# define all(x) (x).begin(), (x).end()
# define rall(x) (x).rbegin(), (x).rend()
# define sz(x) ((int)(x).size())
# define lb lower_bound
# define ub upper_bound
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
# define br break
# define rt return
# define cn continue
# define nl "\n"
# define off exit(0)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll MAXN = 1e6 + 25;
const ll MAXL = 18 + 0;
const ll INF1 = 1e18 + 0;
const ll INF2 = 2e9 + 0;
const ll MOD = 1e9 + 7;
const ll M0D = 998244353;
const ld PI = acos((ld) -1);
const ll P = 67 + 0 + 0;
using namespace std;
using namespace __gnu_pbds;
ll n, m, k, q;
ll g[75][75];
ll dp[75][75][75];
void ma1n ()
{
cin >> n >> m;
for (ll i = 0; i <= n; ++i)
{
for (ll j = 0; j <= n; ++j)
{
g[i][j] = INF1;
}
}
for (ll i = 1, u, v, w; i <= m; ++i)
{
cin >> u >> v >> w;
g[u][v] = min(g[u][v], w);
}
cin >> k >> q;
ll need = min(k, n);
for (ll cur = 0; cur <= need; ++cur)
{
for (ll i = 1; i <= n; ++i)
{
for (ll j = 1; j <= n; ++j)
{
dp[cur][i][j] = INF1;
dp[cur][i][i] = 0;
}
}
}
for (ll cur = 1; cur <= need; ++cur)
{
for (ll i = 1; i <= n; ++i)
{
for (ll j = 1; j <= n; ++j)
{
for (ll v = 1; v <= n; ++v)
{
dp[cur][i][j] = min(dp[cur][i][j], dp[cur - 1][i][v] + g[v][j]);
}
}
}
}
for (ll i = 1, u, v; i <= q; ++i)
{
cin >> u >> v;
cout << (dp[need][u][v] == INF1 ? -1 : dp[need][u][v]) << nl;
}
}
int main (/*?*/)
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
// freopen ("file.in", "r", stdin);
// freopen ("file.out", "w", stdout);
int zxc = 1;
// cin >> zxc;
while (zxc--)
{
ma1n();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
11 ms |
1492 KB |
Output is correct |
8 |
Correct |
27 ms |
3420 KB |
Output is correct |
9 |
Correct |
4 ms |
596 KB |
Output is correct |
10 |
Correct |
16 ms |
2004 KB |
Output is correct |
11 |
Correct |
6 ms |
852 KB |
Output is correct |
12 |
Correct |
19 ms |
2452 KB |
Output is correct |
13 |
Correct |
106 ms |
6620 KB |
Output is correct |
14 |
Correct |
114 ms |
6736 KB |
Output is correct |
15 |
Correct |
123 ms |
9040 KB |
Output is correct |
16 |
Correct |
100 ms |
7372 KB |
Output is correct |
17 |
Correct |
101 ms |
6740 KB |
Output is correct |
18 |
Correct |
102 ms |
6608 KB |
Output is correct |
19 |
Correct |
114 ms |
9548 KB |
Output is correct |
20 |
Correct |
113 ms |
8268 KB |
Output is correct |
21 |
Correct |
98 ms |
6736 KB |
Output is correct |
22 |
Correct |
98 ms |
7112 KB |
Output is correct |
23 |
Correct |
102 ms |
7116 KB |
Output is correct |
24 |
Correct |
104 ms |
8128 KB |
Output is correct |
25 |
Correct |
95 ms |
6860 KB |
Output is correct |
26 |
Correct |
103 ms |
7384 KB |
Output is correct |
27 |
Correct |
105 ms |
7640 KB |
Output is correct |
28 |
Correct |
100 ms |
7508 KB |
Output is correct |
29 |
Correct |
120 ms |
6608 KB |
Output is correct |
30 |
Correct |
102 ms |
7120 KB |
Output is correct |
31 |
Correct |
112 ms |
8016 KB |
Output is correct |
32 |
Correct |
116 ms |
9552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
4 ms |
596 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
596 KB |
Output is correct |
14 |
Correct |
11 ms |
1492 KB |
Output is correct |
15 |
Correct |
27 ms |
3420 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
16 ms |
2004 KB |
Output is correct |
18 |
Correct |
6 ms |
852 KB |
Output is correct |
19 |
Correct |
19 ms |
2452 KB |
Output is correct |
20 |
Correct |
106 ms |
6620 KB |
Output is correct |
21 |
Correct |
114 ms |
6736 KB |
Output is correct |
22 |
Correct |
123 ms |
9040 KB |
Output is correct |
23 |
Correct |
100 ms |
7372 KB |
Output is correct |
24 |
Correct |
101 ms |
6740 KB |
Output is correct |
25 |
Correct |
102 ms |
6608 KB |
Output is correct |
26 |
Correct |
114 ms |
9548 KB |
Output is correct |
27 |
Correct |
113 ms |
8268 KB |
Output is correct |
28 |
Correct |
98 ms |
6736 KB |
Output is correct |
29 |
Correct |
98 ms |
7112 KB |
Output is correct |
30 |
Correct |
102 ms |
7116 KB |
Output is correct |
31 |
Correct |
104 ms |
8128 KB |
Output is correct |
32 |
Correct |
95 ms |
6860 KB |
Output is correct |
33 |
Correct |
103 ms |
7384 KB |
Output is correct |
34 |
Correct |
105 ms |
7640 KB |
Output is correct |
35 |
Correct |
100 ms |
7508 KB |
Output is correct |
36 |
Correct |
120 ms |
6608 KB |
Output is correct |
37 |
Correct |
102 ms |
7120 KB |
Output is correct |
38 |
Correct |
112 ms |
8016 KB |
Output is correct |
39 |
Correct |
116 ms |
9552 KB |
Output is correct |
40 |
Correct |
10 ms |
1236 KB |
Output is correct |
41 |
Correct |
29 ms |
3540 KB |
Output is correct |
42 |
Correct |
5 ms |
724 KB |
Output is correct |
43 |
Correct |
27 ms |
3524 KB |
Output is correct |
44 |
Correct |
7 ms |
848 KB |
Output is correct |
45 |
Correct |
28 ms |
3568 KB |
Output is correct |
46 |
Correct |
96 ms |
6736 KB |
Output is correct |
47 |
Correct |
115 ms |
8012 KB |
Output is correct |
48 |
Correct |
96 ms |
7120 KB |
Output is correct |
49 |
Correct |
126 ms |
9680 KB |
Output is correct |
50 |
Correct |
100 ms |
6732 KB |
Output is correct |
51 |
Correct |
98 ms |
7388 KB |
Output is correct |
52 |
Correct |
98 ms |
6616 KB |
Output is correct |
53 |
Correct |
120 ms |
9800 KB |
Output is correct |
54 |
Correct |
94 ms |
6736 KB |
Output is correct |
55 |
Correct |
107 ms |
6904 KB |
Output is correct |
56 |
Correct |
127 ms |
9416 KB |
Output is correct |
57 |
Correct |
119 ms |
9676 KB |
Output is correct |
58 |
Correct |
101 ms |
6748 KB |
Output is correct |
59 |
Correct |
109 ms |
8016 KB |
Output is correct |
60 |
Correct |
133 ms |
9272 KB |
Output is correct |
61 |
Correct |
117 ms |
9808 KB |
Output is correct |
62 |
Correct |
95 ms |
6600 KB |
Output is correct |
63 |
Correct |
98 ms |
7244 KB |
Output is correct |
64 |
Correct |
102 ms |
7496 KB |
Output is correct |
65 |
Correct |
115 ms |
9548 KB |
Output is correct |