#include <bits/stdc++.h>
using namespace std;
#define lg long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 100;
lg d[N][N][N], cost[N][N], in_queue[N][N][N];
int main()
{
fastio;
lg n, m;
cin >> n >> m;
memset(d, 0x3f, sizeof(d));
memset(cost, 0x3f, sizeof(cost));
for(int i = 0; i < m; i++)
{
lg a, b, t;
cin >> a >> b >> t;
cost[a][b] = min(cost[a][b], t);
}
lg k, q;
cin >> k >> q;
k = min(k, n);
priority_queue<array<lg, 3>> pq;
for(int i = 1; i <= n; i++)
{
pq.push({0, i, 0});
in_queue[i][i][0] = true;
d[i][i][0] = 0;
while(pq.size())
{
lg u = pq.top()[1], v = pq.top()[2];
pq.pop();
in_queue[i][u][v] = false;
if(v == k) continue;
for(int j = 1; j <= n; j++)
{
if(cost[u][j] > 1e6) continue;
if(d[i][j][v+1] > d[i][u][v]+cost[u][j])
{
d[i][j][v+1] = d[i][u][v]+cost[u][j];
if(in_queue[i][j][v+1]) continue;
in_queue[i][j][v+1] = true;
pq.push({-d[i][j][v+1], j, v+1});
}
}
}
for(int j = 1; j <= n; j++)
{
for(int z = 1; z <= n; z++)
{
d[i][j][z] = min(d[i][j][z], d[i][j][z-1]);
}
}
}
while(q--)
{
lg c, e;
cin >> c >> e;
if(d[c][e][k] > 1e9)
{
cout << "-1\n";
continue;
}
cout << d[c][e][k] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11868 KB |
Output is correct |
2 |
Correct |
2 ms |
11868 KB |
Output is correct |
3 |
Correct |
2 ms |
11868 KB |
Output is correct |
4 |
Correct |
2 ms |
11868 KB |
Output is correct |
5 |
Correct |
2 ms |
11728 KB |
Output is correct |
6 |
Correct |
3 ms |
11868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
13148 KB |
Output is correct |
2 |
Correct |
5 ms |
14684 KB |
Output is correct |
3 |
Correct |
6 ms |
14940 KB |
Output is correct |
4 |
Correct |
5 ms |
14940 KB |
Output is correct |
5 |
Correct |
8 ms |
15084 KB |
Output is correct |
6 |
Correct |
9 ms |
14936 KB |
Output is correct |
7 |
Correct |
10 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11868 KB |
Output is correct |
2 |
Correct |
2 ms |
11868 KB |
Output is correct |
3 |
Correct |
2 ms |
11868 KB |
Output is correct |
4 |
Correct |
2 ms |
11868 KB |
Output is correct |
5 |
Correct |
2 ms |
11728 KB |
Output is correct |
6 |
Correct |
3 ms |
11868 KB |
Output is correct |
7 |
Correct |
40 ms |
14936 KB |
Output is correct |
8 |
Correct |
98 ms |
15352 KB |
Output is correct |
9 |
Correct |
9 ms |
14940 KB |
Output is correct |
10 |
Correct |
108 ms |
15072 KB |
Output is correct |
11 |
Correct |
44 ms |
15108 KB |
Output is correct |
12 |
Correct |
562 ms |
15456 KB |
Output is correct |
13 |
Correct |
80 ms |
21352 KB |
Output is correct |
14 |
Correct |
84 ms |
21096 KB |
Output is correct |
15 |
Correct |
746 ms |
21192 KB |
Output is correct |
16 |
Correct |
163 ms |
21076 KB |
Output is correct |
17 |
Correct |
80 ms |
21280 KB |
Output is correct |
18 |
Correct |
83 ms |
21092 KB |
Output is correct |
19 |
Correct |
164 ms |
21328 KB |
Output is correct |
20 |
Correct |
121 ms |
21356 KB |
Output is correct |
21 |
Correct |
76 ms |
21104 KB |
Output is correct |
22 |
Correct |
103 ms |
21328 KB |
Output is correct |
23 |
Correct |
101 ms |
21304 KB |
Output is correct |
24 |
Correct |
177 ms |
21536 KB |
Output is correct |
25 |
Correct |
79 ms |
21332 KB |
Output is correct |
26 |
Correct |
104 ms |
21344 KB |
Output is correct |
27 |
Correct |
115 ms |
21332 KB |
Output is correct |
28 |
Correct |
110 ms |
21584 KB |
Output is correct |
29 |
Correct |
81 ms |
21072 KB |
Output is correct |
30 |
Correct |
113 ms |
21072 KB |
Output is correct |
31 |
Correct |
184 ms |
21072 KB |
Output is correct |
32 |
Correct |
326 ms |
21248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11868 KB |
Output is correct |
2 |
Correct |
2 ms |
11868 KB |
Output is correct |
3 |
Correct |
2 ms |
11868 KB |
Output is correct |
4 |
Correct |
2 ms |
11868 KB |
Output is correct |
5 |
Correct |
2 ms |
11728 KB |
Output is correct |
6 |
Correct |
3 ms |
11868 KB |
Output is correct |
7 |
Correct |
4 ms |
13148 KB |
Output is correct |
8 |
Correct |
5 ms |
14684 KB |
Output is correct |
9 |
Correct |
6 ms |
14940 KB |
Output is correct |
10 |
Correct |
5 ms |
14940 KB |
Output is correct |
11 |
Correct |
8 ms |
15084 KB |
Output is correct |
12 |
Correct |
9 ms |
14936 KB |
Output is correct |
13 |
Correct |
10 ms |
14936 KB |
Output is correct |
14 |
Correct |
40 ms |
14936 KB |
Output is correct |
15 |
Correct |
98 ms |
15352 KB |
Output is correct |
16 |
Correct |
9 ms |
14940 KB |
Output is correct |
17 |
Correct |
108 ms |
15072 KB |
Output is correct |
18 |
Correct |
44 ms |
15108 KB |
Output is correct |
19 |
Correct |
562 ms |
15456 KB |
Output is correct |
20 |
Correct |
80 ms |
21352 KB |
Output is correct |
21 |
Correct |
84 ms |
21096 KB |
Output is correct |
22 |
Correct |
746 ms |
21192 KB |
Output is correct |
23 |
Correct |
163 ms |
21076 KB |
Output is correct |
24 |
Correct |
80 ms |
21280 KB |
Output is correct |
25 |
Correct |
83 ms |
21092 KB |
Output is correct |
26 |
Correct |
164 ms |
21328 KB |
Output is correct |
27 |
Correct |
121 ms |
21356 KB |
Output is correct |
28 |
Correct |
76 ms |
21104 KB |
Output is correct |
29 |
Correct |
103 ms |
21328 KB |
Output is correct |
30 |
Correct |
101 ms |
21304 KB |
Output is correct |
31 |
Correct |
177 ms |
21536 KB |
Output is correct |
32 |
Correct |
79 ms |
21332 KB |
Output is correct |
33 |
Correct |
104 ms |
21344 KB |
Output is correct |
34 |
Correct |
115 ms |
21332 KB |
Output is correct |
35 |
Correct |
110 ms |
21584 KB |
Output is correct |
36 |
Correct |
81 ms |
21072 KB |
Output is correct |
37 |
Correct |
113 ms |
21072 KB |
Output is correct |
38 |
Correct |
184 ms |
21072 KB |
Output is correct |
39 |
Correct |
326 ms |
21248 KB |
Output is correct |
40 |
Correct |
29 ms |
14936 KB |
Output is correct |
41 |
Correct |
100 ms |
15184 KB |
Output is correct |
42 |
Correct |
18 ms |
14940 KB |
Output is correct |
43 |
Correct |
265 ms |
15172 KB |
Output is correct |
44 |
Correct |
41 ms |
15376 KB |
Output is correct |
45 |
Correct |
850 ms |
15376 KB |
Output is correct |
46 |
Correct |
85 ms |
20964 KB |
Output is correct |
47 |
Correct |
206 ms |
21180 KB |
Output is correct |
48 |
Correct |
113 ms |
21072 KB |
Output is correct |
49 |
Correct |
454 ms |
21356 KB |
Output is correct |
50 |
Correct |
80 ms |
21140 KB |
Output is correct |
51 |
Correct |
99 ms |
21072 KB |
Output is correct |
52 |
Correct |
77 ms |
21212 KB |
Output is correct |
53 |
Correct |
171 ms |
21520 KB |
Output is correct |
54 |
Correct |
77 ms |
21228 KB |
Output is correct |
55 |
Correct |
83 ms |
21224 KB |
Output is correct |
56 |
Correct |
286 ms |
21332 KB |
Output is correct |
57 |
Correct |
308 ms |
21328 KB |
Output is correct |
58 |
Correct |
83 ms |
21332 KB |
Output is correct |
59 |
Correct |
122 ms |
21356 KB |
Output is correct |
60 |
Correct |
163 ms |
21328 KB |
Output is correct |
61 |
Correct |
190 ms |
21348 KB |
Output is correct |
62 |
Correct |
77 ms |
21076 KB |
Output is correct |
63 |
Correct |
122 ms |
21388 KB |
Output is correct |
64 |
Correct |
130 ms |
21104 KB |
Output is correct |
65 |
Correct |
418 ms |
21400 KB |
Output is correct |