#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;
using vvii = vector<vector<ii>>;
#define INF 1000000000
int main()
{
int K, n, m, o;
cin >> K >> n >> m >> o;
vvii rev(n);
for (int i = 0; i < m; ++i)
{
int a, b, t;
cin >> a >> b >> t;
rev[b].emplace_back(t, a);
}
vi d0(n, INF);
d0[0] = 0;
for (int u = 1; u < n; ++u)
{
for (ii e : rev[u])
{
d0[u] = min(d0[u], d0[e.second] + e.first);
}
}
for (int i = 0; i < o; ++i)
{
int a, b;
cin >> a >> b;
int ans;
if (a == 0)
{
ans = d0[b];
}
else
{
vi d(n, INF);
d[a] = 0;
for (int u = a + 1; u < n; ++u)
{
for (ii e : rev[u])
{
d[u] = min(d[u], d[e.second] + e.first);
}
}
ans = d[b];
}
cout << (ans == INF ? -1 : ans) << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1737 ms |
4032 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
11 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
384 KB |
Output is correct |
7 |
Correct |
11 ms |
384 KB |
Output is correct |
8 |
Correct |
1733 ms |
3772 KB |
Output is correct |
9 |
Correct |
1682 ms |
3684 KB |
Output is correct |
10 |
Correct |
514 ms |
1972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
31 ms |
384 KB |
Output is correct |
8 |
Correct |
31 ms |
512 KB |
Output is correct |
9 |
Correct |
91 ms |
3320 KB |
Output is correct |
10 |
Correct |
213 ms |
4308 KB |
Output is correct |
11 |
Correct |
154 ms |
3320 KB |
Output is correct |
12 |
Correct |
125 ms |
3192 KB |
Output is correct |
13 |
Correct |
201 ms |
3960 KB |
Output is correct |
14 |
Correct |
122 ms |
2808 KB |
Output is correct |
15 |
Correct |
104 ms |
2424 KB |
Output is correct |
16 |
Correct |
115 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
10 ms |
384 KB |
Output is correct |
9 |
Correct |
9 ms |
384 KB |
Output is correct |
10 |
Correct |
86 ms |
3448 KB |
Output is correct |
11 |
Correct |
148 ms |
3576 KB |
Output is correct |
12 |
Correct |
205 ms |
4352 KB |
Output is correct |
13 |
Correct |
244 ms |
4600 KB |
Output is correct |
14 |
Correct |
182 ms |
4020 KB |
Output is correct |
15 |
Correct |
122 ms |
2680 KB |
Output is correct |
16 |
Correct |
118 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
10 ms |
384 KB |
Output is correct |
9 |
Correct |
9 ms |
384 KB |
Output is correct |
10 |
Correct |
86 ms |
3448 KB |
Output is correct |
11 |
Correct |
148 ms |
3576 KB |
Output is correct |
12 |
Correct |
205 ms |
4352 KB |
Output is correct |
13 |
Correct |
244 ms |
4600 KB |
Output is correct |
14 |
Correct |
182 ms |
4020 KB |
Output is correct |
15 |
Correct |
122 ms |
2680 KB |
Output is correct |
16 |
Correct |
118 ms |
2552 KB |
Output is correct |
17 |
Correct |
822 ms |
3808 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
256 KB |
Output is correct |
23 |
Correct |
24 ms |
384 KB |
Output is correct |
24 |
Correct |
26 ms |
384 KB |
Output is correct |
25 |
Correct |
41 ms |
512 KB |
Output is correct |
26 |
Correct |
33 ms |
384 KB |
Output is correct |
27 |
Correct |
559 ms |
3764 KB |
Output is correct |
28 |
Correct |
1142 ms |
4404 KB |
Output is correct |
29 |
Correct |
1163 ms |
4660 KB |
Output is correct |
30 |
Correct |
1175 ms |
4196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1737 ms |
4032 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
11 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
384 KB |
Output is correct |
7 |
Correct |
11 ms |
384 KB |
Output is correct |
8 |
Correct |
1733 ms |
3772 KB |
Output is correct |
9 |
Correct |
1682 ms |
3684 KB |
Output is correct |
10 |
Correct |
514 ms |
1972 KB |
Output is correct |
11 |
Correct |
157 ms |
3320 KB |
Output is correct |
12 |
Correct |
4 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
31 ms |
384 KB |
Output is correct |
18 |
Correct |
31 ms |
512 KB |
Output is correct |
19 |
Correct |
91 ms |
3320 KB |
Output is correct |
20 |
Correct |
213 ms |
4308 KB |
Output is correct |
21 |
Correct |
154 ms |
3320 KB |
Output is correct |
22 |
Correct |
125 ms |
3192 KB |
Output is correct |
23 |
Correct |
201 ms |
3960 KB |
Output is correct |
24 |
Correct |
122 ms |
2808 KB |
Output is correct |
25 |
Correct |
104 ms |
2424 KB |
Output is correct |
26 |
Correct |
115 ms |
2424 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
256 KB |
Output is correct |
30 |
Correct |
5 ms |
256 KB |
Output is correct |
31 |
Correct |
4 ms |
256 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
7 ms |
384 KB |
Output is correct |
34 |
Correct |
10 ms |
384 KB |
Output is correct |
35 |
Correct |
9 ms |
384 KB |
Output is correct |
36 |
Correct |
86 ms |
3448 KB |
Output is correct |
37 |
Correct |
148 ms |
3576 KB |
Output is correct |
38 |
Correct |
205 ms |
4352 KB |
Output is correct |
39 |
Correct |
244 ms |
4600 KB |
Output is correct |
40 |
Correct |
182 ms |
4020 KB |
Output is correct |
41 |
Correct |
122 ms |
2680 KB |
Output is correct |
42 |
Correct |
118 ms |
2552 KB |
Output is correct |
43 |
Correct |
822 ms |
3808 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
4 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
256 KB |
Output is correct |
49 |
Correct |
24 ms |
384 KB |
Output is correct |
50 |
Correct |
26 ms |
384 KB |
Output is correct |
51 |
Correct |
41 ms |
512 KB |
Output is correct |
52 |
Correct |
33 ms |
384 KB |
Output is correct |
53 |
Correct |
559 ms |
3764 KB |
Output is correct |
54 |
Correct |
1142 ms |
4404 KB |
Output is correct |
55 |
Correct |
1163 ms |
4660 KB |
Output is correct |
56 |
Correct |
1175 ms |
4196 KB |
Output is correct |
57 |
Execution timed out |
3074 ms |
5804 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |