Submission #862583

# Submission time Handle Problem Language Result Execution time Memory
862583 2023-10-18T14:06:04 Z theshadow_04 Toll (BOI17_toll) C++14
100 / 100
116 ms 16828 KB
#include<bits/stdc++.h>

#define F first 
#define S second

using namespace std;

const int maxn = 50005;

struct Query {
	int u, v, id;
};

int n, m, o, k;
vector<pair<int, int> > adj[maxn], rev_adj[maxn];
int ans[maxn], dis[5][maxn][5]; // [start, block, position]

void divide_and_conquer(int l, int r, vector<Query> query) {
	if (l == r) {
		for (auto x : query) {
			ans[x.id] = -1;
		}
		return;
	}
	int mid = (l + r) >> 1;
	for (int i = 0; i < k; ++ i) {
		for (int j = 0; j < k; ++ j) dis[i][mid][j] = 1e9;
		dis[i][mid][i] = 0;
	}
	for (int i = mid; i > l; -- i) {
		for (int j = 0; j < k; ++ j) {
			for (int j1 = 0; j1 < k; ++ j1) dis[j][i - 1][j1] = 1e9;
			for (int j1 = 0; j1 < k; ++ j1) {
				int u = i * k + j1;
				for (auto v : rev_adj[u]) {
					int j2 = v.F % k;
					dis[j][i - 1][j2] = min(dis[j][i - 1][j2], dis[j][i][j1] + v.S);
				}
			}
		}
	}
	for (int i = 0; i < k; ++ i) {
		for (int j = 0; j < k; ++ j) dis[i][mid + 1][j] = 1e9;
		dis[i][mid + 1][i] = 0;
	}
	for (int i = mid + 1; i < r; ++ i) {
		for (int j = 0; j < k; ++ j) {
			for (int j1 = 0; j1 < k; ++ j1) dis[j][i + 1][j1] = 1e9;
			for (int j1 = 0; j1 < k; ++ j1) {
				int u = i * k + j1;
				for (auto v : adj[u]) {
					int j2 = v.F % k;
					dis[j][i + 1][j2] = min(dis[j][i + 1][j2], dis[j][i][j1] + v.S);
				}
			}
		}
	}
	vector<Query> shorter_query[2];
	for (auto p : query) {
		int u = p.u, v = p.v, id = p.id;
		int L = u / k, R = v / k;
		if (L > mid) shorter_query[1].push_back(p);
		else if (R <= mid) shorter_query[0].push_back(p);
		else {
			ans[id] = 1e9;
			for (int i = 0; i < k; ++ i) {
				int u1 = mid * k + i;
				for (auto x : adj[u1]) {
					int v1 = x.F;
					int j = v1 % k;
					ans[id] = min(ans[id], dis[i][L][u % k] + dis[j][R][v % k] + x.S);
				}
			}
			if (ans[id] == 1e9) ans[id] = -1;
		}
	}
	divide_and_conquer(l, mid, shorter_query[0]);
	divide_and_conquer(mid + 1, r, shorter_query[1]);
}

void solve() {
	cin >> k >> n >> m >> o;
	for (int i = 1; i <= m; ++ i) {
		int u, v, w; cin >> u >> v >> w;
		adj[u].push_back({v, w});
		rev_adj[v].push_back({u, w});
	}
	vector<Query> query;
	for (int i = 1; i <= o; ++ i) {
		int u, v; cin >> u >> v;
		query.push_back({u, v, i}); 
	}
	divide_and_conquer(0, (n - 1) / k, query);
	for (int i = 1; i <= o; ++ i) {
		cout << ans[i] << "\n";
	}
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 10260 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 2 ms 3672 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 23 ms 10228 KB Output is correct
9 Correct 26 ms 10012 KB Output is correct
10 Correct 7 ms 6376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 11044 KB Output is correct
2 Correct 1 ms 3416 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 1 ms 5468 KB Output is correct
5 Correct 2 ms 7512 KB Output is correct
6 Correct 2 ms 7516 KB Output is correct
7 Correct 3 ms 4128 KB Output is correct
8 Correct 4 ms 6252 KB Output is correct
9 Correct 27 ms 10268 KB Output is correct
10 Correct 95 ms 15504 KB Output is correct
11 Correct 48 ms 11168 KB Output is correct
12 Correct 42 ms 12620 KB Output is correct
13 Correct 94 ms 15748 KB Output is correct
14 Correct 45 ms 10708 KB Output is correct
15 Correct 46 ms 11612 KB Output is correct
16 Correct 46 ms 11808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 2 ms 5468 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 2 ms 7512 KB Output is correct
5 Correct 2 ms 7516 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 2 ms 5724 KB Output is correct
8 Correct 3 ms 7980 KB Output is correct
9 Correct 3 ms 7772 KB Output is correct
10 Correct 20 ms 9564 KB Output is correct
11 Correct 41 ms 10296 KB Output is correct
12 Correct 69 ms 14932 KB Output is correct
13 Correct 86 ms 15672 KB Output is correct
14 Correct 59 ms 13820 KB Output is correct
15 Correct 48 ms 11616 KB Output is correct
16 Correct 45 ms 11612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 2 ms 5468 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 2 ms 7512 KB Output is correct
5 Correct 2 ms 7516 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 2 ms 5724 KB Output is correct
8 Correct 3 ms 7980 KB Output is correct
9 Correct 3 ms 7772 KB Output is correct
10 Correct 20 ms 9564 KB Output is correct
11 Correct 41 ms 10296 KB Output is correct
12 Correct 69 ms 14932 KB Output is correct
13 Correct 86 ms 15672 KB Output is correct
14 Correct 59 ms 13820 KB Output is correct
15 Correct 48 ms 11616 KB Output is correct
16 Correct 45 ms 11612 KB Output is correct
17 Correct 44 ms 10576 KB Output is correct
18 Correct 1 ms 3420 KB Output is correct
19 Correct 1 ms 5464 KB Output is correct
20 Correct 1 ms 5468 KB Output is correct
21 Correct 1 ms 7516 KB Output is correct
22 Correct 1 ms 7516 KB Output is correct
23 Correct 2 ms 3676 KB Output is correct
24 Correct 2 ms 5724 KB Output is correct
25 Correct 6 ms 7988 KB Output is correct
26 Correct 4 ms 8028 KB Output is correct
27 Correct 23 ms 9796 KB Output is correct
28 Correct 71 ms 15076 KB Output is correct
29 Correct 87 ms 15696 KB Output is correct
30 Correct 60 ms 13916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 10260 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 2 ms 3672 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 23 ms 10228 KB Output is correct
9 Correct 26 ms 10012 KB Output is correct
10 Correct 7 ms 6376 KB Output is correct
11 Correct 44 ms 11044 KB Output is correct
12 Correct 1 ms 3416 KB Output is correct
13 Correct 1 ms 5468 KB Output is correct
14 Correct 1 ms 5468 KB Output is correct
15 Correct 2 ms 7512 KB Output is correct
16 Correct 2 ms 7516 KB Output is correct
17 Correct 3 ms 4128 KB Output is correct
18 Correct 4 ms 6252 KB Output is correct
19 Correct 27 ms 10268 KB Output is correct
20 Correct 95 ms 15504 KB Output is correct
21 Correct 48 ms 11168 KB Output is correct
22 Correct 42 ms 12620 KB Output is correct
23 Correct 94 ms 15748 KB Output is correct
24 Correct 45 ms 10708 KB Output is correct
25 Correct 46 ms 11612 KB Output is correct
26 Correct 46 ms 11808 KB Output is correct
27 Correct 1 ms 3420 KB Output is correct
28 Correct 2 ms 5468 KB Output is correct
29 Correct 1 ms 5468 KB Output is correct
30 Correct 2 ms 7512 KB Output is correct
31 Correct 2 ms 7516 KB Output is correct
32 Correct 1 ms 3676 KB Output is correct
33 Correct 2 ms 5724 KB Output is correct
34 Correct 3 ms 7980 KB Output is correct
35 Correct 3 ms 7772 KB Output is correct
36 Correct 20 ms 9564 KB Output is correct
37 Correct 41 ms 10296 KB Output is correct
38 Correct 69 ms 14932 KB Output is correct
39 Correct 86 ms 15672 KB Output is correct
40 Correct 59 ms 13820 KB Output is correct
41 Correct 48 ms 11616 KB Output is correct
42 Correct 45 ms 11612 KB Output is correct
43 Correct 44 ms 10576 KB Output is correct
44 Correct 1 ms 3420 KB Output is correct
45 Correct 1 ms 5464 KB Output is correct
46 Correct 1 ms 5468 KB Output is correct
47 Correct 1 ms 7516 KB Output is correct
48 Correct 1 ms 7516 KB Output is correct
49 Correct 2 ms 3676 KB Output is correct
50 Correct 2 ms 5724 KB Output is correct
51 Correct 6 ms 7988 KB Output is correct
52 Correct 4 ms 8028 KB Output is correct
53 Correct 23 ms 9796 KB Output is correct
54 Correct 71 ms 15076 KB Output is correct
55 Correct 87 ms 15696 KB Output is correct
56 Correct 60 ms 13916 KB Output is correct
57 Correct 116 ms 16828 KB Output is correct
58 Correct 23 ms 10264 KB Output is correct
59 Correct 46 ms 11072 KB Output is correct