Submission #567695

# Submission time Handle Problem Language Result Execution time Memory
567695 2022-05-24T04:56:52 Z _karan_gandhi Toll (BOI17_toll) C++17
100 / 100
155 ms 199792 KB
#include <bits/stdc++.h>
using namespace std;

#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long

void solve() {
	ll int k, n, m, o; cin >> k >> n >> m >> o;

	const ll int LOG = 20;
	// dp[i][i + 2 ^ k][x][y];
	ll int dp[n / k][LOG][5][5];
	memset(dp, 1, sizeof dp);

	vector<vector<pair<ll int, ll int>>> adj(n);

	for (ll int i = 0; i < m; i++) {
		ll int a, b; cin >> a >> b;
		ll int c; cin >> c;

		adj[a].emplace_back(b, c);
	}

	for (ll int i = 0; i < n; i++) {
		for (auto [v, wt] : adj[i]) {
			dp[i / k][0][i % k][v % k] = wt;
		}
	}

	auto combine = [&](ll int target[5][5], ll int a[5][5], ll int b[5][5]) {
		for (ll int i = 0; i < k; i++) {
			for (ll int j = 0; j < k; j++) {
				for (ll int l = 0; l < k; l++) {
					target[i][j] = min(target[i][j], a[i][l] + b[l][j]);
				}
			}
		}
	};

	for (ll int i = 1; i < LOG; i++) {
		for (ll int j = 0; (j + (1 << i)) < (n + k - 1) / k; j++) {
			combine(dp[j][i], dp[j][i - 1], dp[j + (1 << (i - 1))][i - 1]);
		}
	}

	// TODO: write query again

	auto query = [&](ll int start, ll int end) {
		ll int res[5][5];
		memset(res, 1, sizeof res);
		for (int i = 0; i < 5; i++) res[i][i] = 0;
		ll int level = start / k;
		ll int target = end / k;
		int pow = LOG;

		while (level != target) {
			while (level + (1 << pow) > target) pow--;
			// cout << pl(level) << pl(pow) << endl;

			ll int new_res[5][5];
			memset(new_res, 1, sizeof new_res);

			combine(new_res, res, dp[level][pow]);
			memcpy(res, new_res, sizeof res);

			// for (int ii = 0; ii < k; ii++) {
			// 	for (int jj = 0; jj < k; jj++) {
			// 		cout << pl(ii) << pl(jj) << pl(res[ii][jj]) << endl;
			// 	}
			// }

			level += (1 << pow);
		}

		return res[start % k][end % k];
	};

	while (o--) {
		int a, b; cin >> a >> b;

		ll int res = query(a, b);

		if (res > 1e16) cout << -1 << endl;
		else cout << res << endl;
	}
	
	// auto query = [&](ll int start, ll int levels, ll int en) {
	// 	ll int res[5][5];
	// 	ll int level = start / k;

	// 	memset(res, 1, sizeof res);

	// 	for (ll int i = 0; i < k; i++) res[i][i] = 0;
	// 	ll int pow = LOG;

	// 	while (levels > 0) {
	// 		while (levels < (1 << pow)) pow--;

	// 		ll int new_res[5][5];
	// 		memset(new_res, 1, sizeof new_res);

	// 		combine(new_res, res, dp[level][pow]);
	// 		memcpy(res, new_res, sizeof res);

	// 		level += (1 << pow);
	// 		levels -= (1 << pow);
	// 	}

	// 	return res[start % k][en % k];
	// };

	// while (o--) {
	// 	ll int a, b; cin >> a >> b;
	// 	ll int levels = abs(a / k - b / k);

	// 	if (levels == 0) {
	// 		cout << -1 << endl;
	// 	}

	// 	ll int res = query(a, levels, b);

	// 	if (res > 1e16) {
	// 		cout << -1 << endl;
	// 	} else {
	// 		cout << res << endl;
	// 	}
	// }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	ll int T = 1;
	// cin >> T;
	while (T--)
		solve();
}
# Verdict Execution time Memory Grader output
1 Correct 143 ms 199680 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 4 ms 4300 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 5 ms 4308 KB Output is correct
8 Correct 139 ms 199792 KB Output is correct
9 Correct 128 ms 199516 KB Output is correct
10 Correct 126 ms 197268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 103456 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 8 ms 4308 KB Output is correct
8 Correct 7 ms 2380 KB Output is correct
9 Correct 134 ms 199520 KB Output is correct
10 Correct 115 ms 72908 KB Output is correct
11 Correct 111 ms 103624 KB Output is correct
12 Correct 115 ms 70060 KB Output is correct
13 Correct 118 ms 31560 KB Output is correct
14 Correct 64 ms 44204 KB Output is correct
15 Correct 53 ms 27728 KB Output is correct
16 Correct 54 ms 27724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 3 ms 2408 KB Output is correct
8 Correct 3 ms 1236 KB Output is correct
9 Correct 2 ms 1364 KB Output is correct
10 Correct 132 ms 199492 KB Output is correct
11 Correct 105 ms 103296 KB Output is correct
12 Correct 129 ms 72724 KB Output is correct
13 Correct 118 ms 73448 KB Output is correct
14 Correct 104 ms 71660 KB Output is correct
15 Correct 50 ms 27640 KB Output is correct
16 Correct 71 ms 27596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 3 ms 2408 KB Output is correct
8 Correct 3 ms 1236 KB Output is correct
9 Correct 2 ms 1364 KB Output is correct
10 Correct 132 ms 199492 KB Output is correct
11 Correct 105 ms 103296 KB Output is correct
12 Correct 129 ms 72724 KB Output is correct
13 Correct 118 ms 73448 KB Output is correct
14 Correct 104 ms 71660 KB Output is correct
15 Correct 50 ms 27640 KB Output is correct
16 Correct 71 ms 27596 KB Output is correct
17 Correct 113 ms 103436 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 4 ms 4308 KB Output is correct
24 Correct 3 ms 2388 KB Output is correct
25 Correct 5 ms 1232 KB Output is correct
26 Correct 6 ms 1480 KB Output is correct
27 Correct 155 ms 199580 KB Output is correct
28 Correct 107 ms 72808 KB Output is correct
29 Correct 116 ms 73512 KB Output is correct
30 Correct 110 ms 71732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 199680 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 4 ms 4300 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 5 ms 4308 KB Output is correct
8 Correct 139 ms 199792 KB Output is correct
9 Correct 128 ms 199516 KB Output is correct
10 Correct 126 ms 197268 KB Output is correct
11 Correct 126 ms 103456 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 8 ms 4308 KB Output is correct
18 Correct 7 ms 2380 KB Output is correct
19 Correct 134 ms 199520 KB Output is correct
20 Correct 115 ms 72908 KB Output is correct
21 Correct 111 ms 103624 KB Output is correct
22 Correct 115 ms 70060 KB Output is correct
23 Correct 118 ms 31560 KB Output is correct
24 Correct 64 ms 44204 KB Output is correct
25 Correct 53 ms 27728 KB Output is correct
26 Correct 54 ms 27724 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 324 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 3 ms 4180 KB Output is correct
33 Correct 3 ms 2408 KB Output is correct
34 Correct 3 ms 1236 KB Output is correct
35 Correct 2 ms 1364 KB Output is correct
36 Correct 132 ms 199492 KB Output is correct
37 Correct 105 ms 103296 KB Output is correct
38 Correct 129 ms 72724 KB Output is correct
39 Correct 118 ms 73448 KB Output is correct
40 Correct 104 ms 71660 KB Output is correct
41 Correct 50 ms 27640 KB Output is correct
42 Correct 71 ms 27596 KB Output is correct
43 Correct 113 ms 103436 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 316 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 328 KB Output is correct
49 Correct 4 ms 4308 KB Output is correct
50 Correct 3 ms 2388 KB Output is correct
51 Correct 5 ms 1232 KB Output is correct
52 Correct 6 ms 1480 KB Output is correct
53 Correct 155 ms 199580 KB Output is correct
54 Correct 107 ms 72808 KB Output is correct
55 Correct 116 ms 73512 KB Output is correct
56 Correct 110 ms 71732 KB Output is correct
57 Correct 153 ms 58128 KB Output is correct
58 Correct 141 ms 199640 KB Output is correct
59 Correct 123 ms 103760 KB Output is correct