#include <bits/stdc++.h>
#define ll long long
#define pb push_back
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) (int)x.size()
#define fi first
#define sd second
#define all(x) x.begin(), x.end()
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 75;
//vector< pair<int, int> > g[N];
int n, m, q, k, F;
bool used[N];
ll dst[N][N][N], g[N][N];
/*
void dfs(int v, int skok, ll sum)
{
if (skok > k || sum > ans) return;
if (v == F)
{
ans = min(ans, sum);
return;
}
for(auto [u, w] : g[v])
{
dfs(u, skok + 1, sum + w);
}
}
*/
void calc(int x)
{
for(int i = 1; i <= n; i++)
for(int j = 0; j <= k; j++)
dst[x][i][j] = 1e18;
dst[x][x][0] = 0;
set< pair<ll, pair<int, int> > > se;
se.insert({0, {0, x}});
while(sz(se) > 0)
{
auto c = *se.begin(); se.erase(se.begin());
ll d = c.fi;
int v = c.sd.sd, skok = c.sd.fi;
if (dst[x][v][skok] < d) continue;
for(int u = 1; u <= n; u++)
{
if (g[v][u] == 1e18) continue;
ll w = g[v][u];
if (skok + 1 <= k && dst[x][v][skok] + w < dst[x][u][skok + 1])
{
dst[x][u][skok + 1] = dst[x][v][skok] + w;
se.insert({dst[x][u][skok + 1], {skok + 1, u}});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
g[i][j] = 1e18;
for(int i = 1; i <= m; i++)
{
int u, v;
ll z;
cin >> u >> v >> z;
g[u][v] = min(g[u][v], z);
}
cin >> k >> q;
k = min(k, n);
for(int i = 1; i <= n; i++)
calc(i);
while(q--)
{
int S;
cin >> S >> F;
ll ans = 1e18;
for(int i = 0; i <= k; i++) ans = min(ans, dst[S][F][i]);
if (ans == 1e18) cout << -1 << '\n';
else cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3404 KB |
Output is correct |
2 |
Correct |
3 ms |
3404 KB |
Output is correct |
3 |
Correct |
6 ms |
3404 KB |
Output is correct |
4 |
Correct |
6 ms |
3580 KB |
Output is correct |
5 |
Correct |
9 ms |
3376 KB |
Output is correct |
6 |
Correct |
9 ms |
3404 KB |
Output is correct |
7 |
Correct |
14 ms |
3340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
56 ms |
3424 KB |
Output is correct |
8 |
Correct |
170 ms |
3780 KB |
Output is correct |
9 |
Correct |
12 ms |
3404 KB |
Output is correct |
10 |
Correct |
129 ms |
3476 KB |
Output is correct |
11 |
Correct |
55 ms |
3508 KB |
Output is correct |
12 |
Correct |
232 ms |
3744 KB |
Output is correct |
13 |
Correct |
145 ms |
3412 KB |
Output is correct |
14 |
Correct |
111 ms |
3424 KB |
Output is correct |
15 |
Correct |
349 ms |
4048 KB |
Output is correct |
16 |
Correct |
230 ms |
9316 KB |
Output is correct |
17 |
Correct |
99 ms |
5064 KB |
Output is correct |
18 |
Correct |
109 ms |
7224 KB |
Output is correct |
19 |
Correct |
284 ms |
7472 KB |
Output is correct |
20 |
Correct |
201 ms |
8512 KB |
Output is correct |
21 |
Correct |
132 ms |
7488 KB |
Output is correct |
22 |
Correct |
147 ms |
8900 KB |
Output is correct |
23 |
Correct |
141 ms |
8904 KB |
Output is correct |
24 |
Correct |
252 ms |
7748 KB |
Output is correct |
25 |
Correct |
101 ms |
7364 KB |
Output is correct |
26 |
Correct |
149 ms |
6696 KB |
Output is correct |
27 |
Correct |
183 ms |
3544 KB |
Output is correct |
28 |
Correct |
158 ms |
3428 KB |
Output is correct |
29 |
Correct |
101 ms |
3440 KB |
Output is correct |
30 |
Correct |
158 ms |
7876 KB |
Output is correct |
31 |
Correct |
266 ms |
9832 KB |
Output is correct |
32 |
Correct |
505 ms |
9944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
3404 KB |
Output is correct |
8 |
Correct |
3 ms |
3404 KB |
Output is correct |
9 |
Correct |
6 ms |
3404 KB |
Output is correct |
10 |
Correct |
6 ms |
3580 KB |
Output is correct |
11 |
Correct |
9 ms |
3376 KB |
Output is correct |
12 |
Correct |
9 ms |
3404 KB |
Output is correct |
13 |
Correct |
14 ms |
3340 KB |
Output is correct |
14 |
Correct |
56 ms |
3424 KB |
Output is correct |
15 |
Correct |
170 ms |
3780 KB |
Output is correct |
16 |
Correct |
12 ms |
3404 KB |
Output is correct |
17 |
Correct |
129 ms |
3476 KB |
Output is correct |
18 |
Correct |
55 ms |
3508 KB |
Output is correct |
19 |
Correct |
232 ms |
3744 KB |
Output is correct |
20 |
Correct |
145 ms |
3412 KB |
Output is correct |
21 |
Correct |
111 ms |
3424 KB |
Output is correct |
22 |
Correct |
349 ms |
4048 KB |
Output is correct |
23 |
Correct |
230 ms |
9316 KB |
Output is correct |
24 |
Correct |
99 ms |
5064 KB |
Output is correct |
25 |
Correct |
109 ms |
7224 KB |
Output is correct |
26 |
Correct |
284 ms |
7472 KB |
Output is correct |
27 |
Correct |
201 ms |
8512 KB |
Output is correct |
28 |
Correct |
132 ms |
7488 KB |
Output is correct |
29 |
Correct |
147 ms |
8900 KB |
Output is correct |
30 |
Correct |
141 ms |
8904 KB |
Output is correct |
31 |
Correct |
252 ms |
7748 KB |
Output is correct |
32 |
Correct |
101 ms |
7364 KB |
Output is correct |
33 |
Correct |
149 ms |
6696 KB |
Output is correct |
34 |
Correct |
183 ms |
3544 KB |
Output is correct |
35 |
Correct |
158 ms |
3428 KB |
Output is correct |
36 |
Correct |
101 ms |
3440 KB |
Output is correct |
37 |
Correct |
158 ms |
7876 KB |
Output is correct |
38 |
Correct |
266 ms |
9832 KB |
Output is correct |
39 |
Correct |
505 ms |
9944 KB |
Output is correct |
40 |
Correct |
40 ms |
3400 KB |
Output is correct |
41 |
Correct |
157 ms |
3548 KB |
Output is correct |
42 |
Correct |
28 ms |
3476 KB |
Output is correct |
43 |
Correct |
275 ms |
3468 KB |
Output is correct |
44 |
Correct |
50 ms |
3552 KB |
Output is correct |
45 |
Correct |
393 ms |
4032 KB |
Output is correct |
46 |
Correct |
132 ms |
4288 KB |
Output is correct |
47 |
Correct |
248 ms |
7120 KB |
Output is correct |
48 |
Correct |
169 ms |
6468 KB |
Output is correct |
49 |
Correct |
423 ms |
9316 KB |
Output is correct |
50 |
Correct |
116 ms |
7516 KB |
Output is correct |
51 |
Correct |
147 ms |
7996 KB |
Output is correct |
52 |
Correct |
99 ms |
8732 KB |
Output is correct |
53 |
Correct |
326 ms |
8804 KB |
Output is correct |
54 |
Correct |
107 ms |
7632 KB |
Output is correct |
55 |
Correct |
121 ms |
6600 KB |
Output is correct |
56 |
Correct |
345 ms |
9444 KB |
Output is correct |
57 |
Correct |
378 ms |
6188 KB |
Output is correct |
58 |
Correct |
97 ms |
9668 KB |
Output is correct |
59 |
Correct |
228 ms |
9596 KB |
Output is correct |
60 |
Correct |
329 ms |
9928 KB |
Output is correct |
61 |
Correct |
363 ms |
9644 KB |
Output is correct |
62 |
Correct |
131 ms |
9484 KB |
Output is correct |
63 |
Correct |
204 ms |
9716 KB |
Output is correct |
64 |
Correct |
194 ms |
4460 KB |
Output is correct |
65 |
Correct |
492 ms |
8116 KB |
Output is correct |