Submission #1108272

# Submission time Handle Problem Language Result Execution time Memory
1108272 2024-11-03T15:17:28 Z vjudge1 Toll (BOI17_toll) C++17
100 / 100
171 ms 67968 KB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 7;

struct matrix {
	vector<vector<int>> data;
	
	matrix() {}
	matrix(int n, int m) : data(n, vector<int>(m)) {}
	matrix(vector<vector<int>>& v) : data(v) {}
	
	int row() const {return data.size();}
	int col() const {return data[0].size();}
	auto & operator [] (int i) {return data[i];}
	const auto & operator [] (int i) const {return data[i];}
	
	friend matrix operator * (const matrix& a, const matrix& b) {
		matrix res(a.row(), b.col());
		for (int i = 0; i < a.row(); ++i)
			for (int j = 0; j < b.col(); ++j) {
				res[i][j] = inf;
				for (int k = 0; k < b.row(); ++k)
					res[i][j] = min(res[i][j], a[i][k] + b[k][j]);
			}
		return res;
	}	
	
	friend ostream& operator << (ostream& out, const matrix& o) {
		for (auto r : o.data) {
			for (int i : r) out << i << ' ';
			out << '\n';
		}
		out << '\n';
		return out;
	}
};

vector<pair<int, int>> radj[50008];
matrix trans[50008][16];


void build_binlift(int n) {
	for (int lg = 1; (1 << lg) <= n; ++lg) for (int i = 0; i + (1 << lg) - 1 < n; ++i)
		trans[i][lg] = trans[i][lg - 1] * trans[i + (1 << (lg - 1))][lg - 1];
}

matrix identity_matrix(int n) {
	matrix I(n, n);
	for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) I[i][j] = (i == j ? 0 : inf);
	return I;
}

int k;
matrix res; int pt, dist;
matrix query(int l, int r) {
	res = identity_matrix(k); pt = l; dist = r - l + 1;
	for (int bit = 0; bit < 16; ++bit) if (dist & (1 << bit)) {
		res = res * trans[pt][bit];
		pt += (1 << bit);
	}
	return res;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, m, q, u, v, w; cin >> k >> n >> m >> q; 
	if (n % k) n = n - n % k + k;
	while (m--) cin >> u >> v >> w, radj[v].push_back({u, w});
	for (int b = 0; b < n / k; ++b) {
		trans[b][0] = matrix(k, k);
		for (int i = 0; i < k; ++i) {
			for (int j = 0; j < k; ++j) trans[b][0][j][i] = inf;
			for (pair<int, int> r_edge : radj[b * k + i]) trans[b][0][r_edge.first % k][i] = r_edge.second;
		}
	}
	build_binlift(n / k); matrix T, dp;
	while (q--) {
		cin >> u >> v;
		if (u == v) cout << 0 << '\n';
		else if (u / k + 1 > v / k) cout << -1 << '\n';
		else {
			T = query(u / k + 1, v / k);
			dp = matrix(1, k);
			for (int i = u - u % k; i < u - u % k + k; ++i) dp[0][i % k] = (i == u ? 0 : inf);
			dp = dp * T;
			cout << (dp[0][v % k] == inf ? -1 : dp[0][v % k]) << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 125 ms 67920 KB Output is correct
2 Correct 8 ms 20304 KB Output is correct
3 Correct 7 ms 20472 KB Output is correct
4 Correct 10 ms 20304 KB Output is correct
5 Correct 9 ms 20988 KB Output is correct
6 Correct 9 ms 20816 KB Output is correct
7 Correct 9 ms 21020 KB Output is correct
8 Correct 120 ms 67964 KB Output is correct
9 Correct 114 ms 67912 KB Output is correct
10 Correct 106 ms 66376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 64840 KB Output is correct
2 Correct 8 ms 20304 KB Output is correct
3 Correct 9 ms 20304 KB Output is correct
4 Correct 7 ms 20304 KB Output is correct
5 Correct 10 ms 20424 KB Output is correct
6 Correct 7 ms 20304 KB Output is correct
7 Correct 14 ms 21044 KB Output is correct
8 Correct 16 ms 20816 KB Output is correct
9 Correct 98 ms 67920 KB Output is correct
10 Correct 142 ms 59976 KB Output is correct
11 Correct 106 ms 64840 KB Output is correct
12 Correct 114 ms 59216 KB Output is correct
13 Correct 92 ms 42568 KB Output is correct
14 Correct 71 ms 43080 KB Output is correct
15 Correct 72 ms 41288 KB Output is correct
16 Correct 79 ms 41296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 20304 KB Output is correct
2 Correct 7 ms 20408 KB Output is correct
3 Correct 7 ms 20304 KB Output is correct
4 Correct 7 ms 20304 KB Output is correct
5 Correct 7 ms 20468 KB Output is correct
6 Correct 8 ms 20984 KB Output is correct
7 Correct 8 ms 20816 KB Output is correct
8 Correct 9 ms 20752 KB Output is correct
9 Correct 8 ms 20816 KB Output is correct
10 Correct 90 ms 67960 KB Output is correct
11 Correct 109 ms 64584 KB Output is correct
12 Correct 125 ms 59860 KB Output is correct
13 Correct 123 ms 60136 KB Output is correct
14 Correct 131 ms 59724 KB Output is correct
15 Correct 73 ms 41032 KB Output is correct
16 Correct 78 ms 41116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 20304 KB Output is correct
2 Correct 7 ms 20408 KB Output is correct
3 Correct 7 ms 20304 KB Output is correct
4 Correct 7 ms 20304 KB Output is correct
5 Correct 7 ms 20468 KB Output is correct
6 Correct 8 ms 20984 KB Output is correct
7 Correct 8 ms 20816 KB Output is correct
8 Correct 9 ms 20752 KB Output is correct
9 Correct 8 ms 20816 KB Output is correct
10 Correct 90 ms 67960 KB Output is correct
11 Correct 109 ms 64584 KB Output is correct
12 Correct 125 ms 59860 KB Output is correct
13 Correct 123 ms 60136 KB Output is correct
14 Correct 131 ms 59724 KB Output is correct
15 Correct 73 ms 41032 KB Output is correct
16 Correct 78 ms 41116 KB Output is correct
17 Correct 110 ms 64840 KB Output is correct
18 Correct 10 ms 20220 KB Output is correct
19 Correct 7 ms 20304 KB Output is correct
20 Correct 8 ms 20304 KB Output is correct
21 Correct 8 ms 20316 KB Output is correct
22 Correct 8 ms 20420 KB Output is correct
23 Correct 11 ms 20816 KB Output is correct
24 Correct 11 ms 20816 KB Output is correct
25 Correct 14 ms 20816 KB Output is correct
26 Correct 12 ms 20816 KB Output is correct
27 Correct 106 ms 67968 KB Output is correct
28 Correct 143 ms 59976 KB Output is correct
29 Correct 146 ms 60132 KB Output is correct
30 Correct 129 ms 59616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 67920 KB Output is correct
2 Correct 8 ms 20304 KB Output is correct
3 Correct 7 ms 20472 KB Output is correct
4 Correct 10 ms 20304 KB Output is correct
5 Correct 9 ms 20988 KB Output is correct
6 Correct 9 ms 20816 KB Output is correct
7 Correct 9 ms 21020 KB Output is correct
8 Correct 120 ms 67964 KB Output is correct
9 Correct 114 ms 67912 KB Output is correct
10 Correct 106 ms 66376 KB Output is correct
11 Correct 123 ms 64840 KB Output is correct
12 Correct 8 ms 20304 KB Output is correct
13 Correct 9 ms 20304 KB Output is correct
14 Correct 7 ms 20304 KB Output is correct
15 Correct 10 ms 20424 KB Output is correct
16 Correct 7 ms 20304 KB Output is correct
17 Correct 14 ms 21044 KB Output is correct
18 Correct 16 ms 20816 KB Output is correct
19 Correct 98 ms 67920 KB Output is correct
20 Correct 142 ms 59976 KB Output is correct
21 Correct 106 ms 64840 KB Output is correct
22 Correct 114 ms 59216 KB Output is correct
23 Correct 92 ms 42568 KB Output is correct
24 Correct 71 ms 43080 KB Output is correct
25 Correct 72 ms 41288 KB Output is correct
26 Correct 79 ms 41296 KB Output is correct
27 Correct 7 ms 20304 KB Output is correct
28 Correct 7 ms 20408 KB Output is correct
29 Correct 7 ms 20304 KB Output is correct
30 Correct 7 ms 20304 KB Output is correct
31 Correct 7 ms 20468 KB Output is correct
32 Correct 8 ms 20984 KB Output is correct
33 Correct 8 ms 20816 KB Output is correct
34 Correct 9 ms 20752 KB Output is correct
35 Correct 8 ms 20816 KB Output is correct
36 Correct 90 ms 67960 KB Output is correct
37 Correct 109 ms 64584 KB Output is correct
38 Correct 125 ms 59860 KB Output is correct
39 Correct 123 ms 60136 KB Output is correct
40 Correct 131 ms 59724 KB Output is correct
41 Correct 73 ms 41032 KB Output is correct
42 Correct 78 ms 41116 KB Output is correct
43 Correct 110 ms 64840 KB Output is correct
44 Correct 10 ms 20220 KB Output is correct
45 Correct 7 ms 20304 KB Output is correct
46 Correct 8 ms 20304 KB Output is correct
47 Correct 8 ms 20316 KB Output is correct
48 Correct 8 ms 20420 KB Output is correct
49 Correct 11 ms 20816 KB Output is correct
50 Correct 11 ms 20816 KB Output is correct
51 Correct 14 ms 20816 KB Output is correct
52 Correct 12 ms 20816 KB Output is correct
53 Correct 106 ms 67968 KB Output is correct
54 Correct 143 ms 59976 KB Output is correct
55 Correct 146 ms 60132 KB Output is correct
56 Correct 129 ms 59616 KB Output is correct
57 Correct 171 ms 60232 KB Output is correct
58 Correct 127 ms 67808 KB Output is correct
59 Correct 145 ms 64792 KB Output is correct