#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
void FastIO(string name = "")
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
if (name.empty() == 0)
{
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
void solve()
{
int k, n, m, o;
cin >> k >> n >> m >> o;
vector<vector<pair<int, int>>> nei(n);
for (int i = 1; i <= m; i++)
{
int a, b, t;
cin >> a >> b >> t;
nei[a].push_back({b, t});
}
int lg = log2(n);
const long long inf = 1e11;
vector table(n, vector(lg + 1, vector(5, inf)));
for (int i = 0; i < n; i++)
{
for (auto &[to, toll] : nei[i])
{
table[i][0][to % k] = toll;
}
}
for (int bit = 1; bit <= lg; bit++)
{
for (int i = 0; i < n; i++)
{
int jump = (1 << (bit - 1));
int ind = (i / k + jump ) * k ;
for (int rem = 0; rem < k; rem++)
{
if (ind >= n)
break;
for (int j = 0; j < k; j++)
table[i][bit][j] = min(
table[i][bit-1][rem]+table[ind][bit - 1][j],
table[i][bit][j]);
ind++;
}
}
}
for (int q = 1; q <= o; q++)
{
int a, b;
cin >> a >> b;
if (a == b)
{
cout << 0 << endl;
}
else if (b/k <= a/k)
{
cout << -1 << endl;
}
else
{
int length = (b / k) - (a / k);
vector<pair<int, long long>> node{{a, 0}};
for (int bit = 0; bit <= lg; bit++)
if (length >> bit & 1)
{
map<int, long long> dis;
for (auto &[at, d] : node)
{
int ind = (at / k + (1<<bit) ) * k ;
for (int re = 0; re < k; re++)
{
if (dis.count(ind))
dis[ind] = min(dis[ind], d + table[at][bit][re]);
else
dis[ind] = d + table[at][bit][re];
ind++;
}
}
node.clear();
for (auto &e : dis)
node.push_back(e);
}
long long ans = 2e13;
for (auto &[at, ds] : node)
if (at == b)
{
ans = ds;
}
cout << (ans >= (int)1e9 ? -1 : ans) << endl;
}
}
}
int main()
{
FastIO("");
int t = 1;
//cin >> t;
for (int i = 0; i < t; i++)
{
solve();
}
}
Compilation message
toll.cpp: In function 'void FastIO(std::string)':
toll.cpp:14:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:15:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
62292 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
2 ms |
1116 KB |
Output is correct |
7 |
Correct |
2 ms |
1120 KB |
Output is correct |
8 |
Correct |
98 ms |
62544 KB |
Output is correct |
9 |
Correct |
90 ms |
62292 KB |
Output is correct |
10 |
Correct |
86 ms |
59964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
62888 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
1336 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
86 ms |
62380 KB |
Output is correct |
10 |
Correct |
128 ms |
64596 KB |
Output is correct |
11 |
Correct |
127 ms |
63056 KB |
Output is correct |
12 |
Correct |
108 ms |
62544 KB |
Output is correct |
13 |
Correct |
104 ms |
38764 KB |
Output is correct |
14 |
Correct |
64 ms |
36688 KB |
Output is correct |
15 |
Correct |
99 ms |
36180 KB |
Output is correct |
16 |
Correct |
81 ms |
36176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
1232 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
64 ms |
62292 KB |
Output is correct |
11 |
Correct |
118 ms |
62804 KB |
Output is correct |
12 |
Correct |
110 ms |
64500 KB |
Output is correct |
13 |
Correct |
114 ms |
64852 KB |
Output is correct |
14 |
Correct |
103 ms |
63824 KB |
Output is correct |
15 |
Correct |
73 ms |
36252 KB |
Output is correct |
16 |
Correct |
91 ms |
36176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
1232 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
64 ms |
62292 KB |
Output is correct |
11 |
Correct |
118 ms |
62804 KB |
Output is correct |
12 |
Correct |
110 ms |
64500 KB |
Output is correct |
13 |
Correct |
114 ms |
64852 KB |
Output is correct |
14 |
Correct |
103 ms |
63824 KB |
Output is correct |
15 |
Correct |
73 ms |
36252 KB |
Output is correct |
16 |
Correct |
91 ms |
36176 KB |
Output is correct |
17 |
Correct |
90 ms |
63064 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
1112 KB |
Output is correct |
25 |
Correct |
5 ms |
1372 KB |
Output is correct |
26 |
Correct |
4 ms |
1356 KB |
Output is correct |
27 |
Correct |
86 ms |
62296 KB |
Output is correct |
28 |
Correct |
116 ms |
64416 KB |
Output is correct |
29 |
Correct |
120 ms |
64932 KB |
Output is correct |
30 |
Correct |
117 ms |
63828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
62292 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
2 ms |
1116 KB |
Output is correct |
7 |
Correct |
2 ms |
1120 KB |
Output is correct |
8 |
Correct |
98 ms |
62544 KB |
Output is correct |
9 |
Correct |
90 ms |
62292 KB |
Output is correct |
10 |
Correct |
86 ms |
59964 KB |
Output is correct |
11 |
Correct |
104 ms |
62888 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
1336 KB |
Output is correct |
18 |
Correct |
7 ms |
1372 KB |
Output is correct |
19 |
Correct |
86 ms |
62380 KB |
Output is correct |
20 |
Correct |
128 ms |
64596 KB |
Output is correct |
21 |
Correct |
127 ms |
63056 KB |
Output is correct |
22 |
Correct |
108 ms |
62544 KB |
Output is correct |
23 |
Correct |
104 ms |
38764 KB |
Output is correct |
24 |
Correct |
64 ms |
36688 KB |
Output is correct |
25 |
Correct |
99 ms |
36180 KB |
Output is correct |
26 |
Correct |
81 ms |
36176 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
456 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
1232 KB |
Output is correct |
33 |
Correct |
1 ms |
1116 KB |
Output is correct |
34 |
Correct |
2 ms |
1116 KB |
Output is correct |
35 |
Correct |
3 ms |
1116 KB |
Output is correct |
36 |
Correct |
64 ms |
62292 KB |
Output is correct |
37 |
Correct |
118 ms |
62804 KB |
Output is correct |
38 |
Correct |
110 ms |
64500 KB |
Output is correct |
39 |
Correct |
114 ms |
64852 KB |
Output is correct |
40 |
Correct |
103 ms |
63824 KB |
Output is correct |
41 |
Correct |
73 ms |
36252 KB |
Output is correct |
42 |
Correct |
91 ms |
36176 KB |
Output is correct |
43 |
Correct |
90 ms |
63064 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
3 ms |
1116 KB |
Output is correct |
50 |
Correct |
3 ms |
1112 KB |
Output is correct |
51 |
Correct |
5 ms |
1372 KB |
Output is correct |
52 |
Correct |
4 ms |
1356 KB |
Output is correct |
53 |
Correct |
86 ms |
62296 KB |
Output is correct |
54 |
Correct |
116 ms |
64416 KB |
Output is correct |
55 |
Correct |
120 ms |
64932 KB |
Output is correct |
56 |
Correct |
117 ms |
63828 KB |
Output is correct |
57 |
Correct |
225 ms |
65772 KB |
Output is correct |
58 |
Correct |
87 ms |
62288 KB |
Output is correct |
59 |
Correct |
115 ms |
63316 KB |
Output is correct |