답안 #1115818

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1115818 2024-11-21T01:54:48 Z vjudge1 Toll (BOI17_toll) C++17
100 / 100
170 ms 67920 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';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 67884 KB Output is correct
2 Correct 8 ms 20304 KB Output is correct
3 Correct 7 ms 20304 KB Output is correct
4 Correct 7 ms 20304 KB Output is correct
5 Correct 9 ms 20816 KB Output is correct
6 Correct 10 ms 20816 KB Output is correct
7 Correct 12 ms 20816 KB Output is correct
8 Correct 118 ms 67920 KB Output is correct
9 Correct 117 ms 67812 KB Output is correct
10 Correct 102 ms 66376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 64704 KB Output is correct
2 Correct 8 ms 20476 KB Output is correct
3 Correct 8 ms 20304 KB Output is correct
4 Correct 7 ms 20304 KB Output is correct
5 Correct 8 ms 20424 KB Output is correct
6 Correct 8 ms 20304 KB Output is correct
7 Correct 14 ms 21020 KB Output is correct
8 Correct 16 ms 20816 KB Output is correct
9 Correct 105 ms 67912 KB Output is correct
10 Correct 143 ms 59976 KB Output is correct
11 Correct 112 ms 64704 KB Output is correct
12 Correct 135 ms 59216 KB Output is correct
13 Correct 97 ms 42568 KB Output is correct
14 Correct 85 ms 43080 KB Output is correct
15 Correct 83 ms 41108 KB Output is correct
16 Correct 84 ms 41288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 20304 KB Output is correct
2 Correct 7 ms 20304 KB Output is correct
3 Correct 8 ms 20424 KB Output is correct
4 Correct 7 ms 20316 KB Output is correct
5 Correct 7 ms 20304 KB Output is correct
6 Correct 9 ms 20816 KB Output is correct
7 Correct 9 ms 20984 KB Output is correct
8 Correct 9 ms 20816 KB Output is correct
9 Correct 10 ms 20816 KB Output is correct
10 Correct 97 ms 67880 KB Output is correct
11 Correct 97 ms 64584 KB Output is correct
12 Correct 123 ms 59976 KB Output is correct
13 Correct 131 ms 60232 KB Output is correct
14 Correct 112 ms 59484 KB Output is correct
15 Correct 76 ms 41032 KB Output is correct
16 Correct 68 ms 41216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 20304 KB Output is correct
2 Correct 7 ms 20304 KB Output is correct
3 Correct 8 ms 20424 KB Output is correct
4 Correct 7 ms 20316 KB Output is correct
5 Correct 7 ms 20304 KB Output is correct
6 Correct 9 ms 20816 KB Output is correct
7 Correct 9 ms 20984 KB Output is correct
8 Correct 9 ms 20816 KB Output is correct
9 Correct 10 ms 20816 KB Output is correct
10 Correct 97 ms 67880 KB Output is correct
11 Correct 97 ms 64584 KB Output is correct
12 Correct 123 ms 59976 KB Output is correct
13 Correct 131 ms 60232 KB Output is correct
14 Correct 112 ms 59484 KB Output is correct
15 Correct 76 ms 41032 KB Output is correct
16 Correct 68 ms 41216 KB Output is correct
17 Correct 105 ms 64840 KB Output is correct
18 Correct 7 ms 20316 KB Output is correct
19 Correct 7 ms 20304 KB Output is correct
20 Correct 7 ms 20472 KB Output is correct
21 Correct 7 ms 20304 KB Output is correct
22 Correct 10 ms 20228 KB Output is correct
23 Correct 9 ms 20816 KB Output is correct
24 Correct 10 ms 20816 KB Output is correct
25 Correct 12 ms 21068 KB Output is correct
26 Correct 12 ms 20816 KB Output is correct
27 Correct 95 ms 67912 KB Output is correct
28 Correct 123 ms 59980 KB Output is correct
29 Correct 123 ms 60232 KB Output is correct
30 Correct 115 ms 59728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 67884 KB Output is correct
2 Correct 8 ms 20304 KB Output is correct
3 Correct 7 ms 20304 KB Output is correct
4 Correct 7 ms 20304 KB Output is correct
5 Correct 9 ms 20816 KB Output is correct
6 Correct 10 ms 20816 KB Output is correct
7 Correct 12 ms 20816 KB Output is correct
8 Correct 118 ms 67920 KB Output is correct
9 Correct 117 ms 67812 KB Output is correct
10 Correct 102 ms 66376 KB Output is correct
11 Correct 119 ms 64704 KB Output is correct
12 Correct 8 ms 20476 KB Output is correct
13 Correct 8 ms 20304 KB Output is correct
14 Correct 7 ms 20304 KB Output is correct
15 Correct 8 ms 20424 KB Output is correct
16 Correct 8 ms 20304 KB Output is correct
17 Correct 14 ms 21020 KB Output is correct
18 Correct 16 ms 20816 KB Output is correct
19 Correct 105 ms 67912 KB Output is correct
20 Correct 143 ms 59976 KB Output is correct
21 Correct 112 ms 64704 KB Output is correct
22 Correct 135 ms 59216 KB Output is correct
23 Correct 97 ms 42568 KB Output is correct
24 Correct 85 ms 43080 KB Output is correct
25 Correct 83 ms 41108 KB Output is correct
26 Correct 84 ms 41288 KB Output is correct
27 Correct 7 ms 20304 KB Output is correct
28 Correct 7 ms 20304 KB Output is correct
29 Correct 8 ms 20424 KB Output is correct
30 Correct 7 ms 20316 KB Output is correct
31 Correct 7 ms 20304 KB Output is correct
32 Correct 9 ms 20816 KB Output is correct
33 Correct 9 ms 20984 KB Output is correct
34 Correct 9 ms 20816 KB Output is correct
35 Correct 10 ms 20816 KB Output is correct
36 Correct 97 ms 67880 KB Output is correct
37 Correct 97 ms 64584 KB Output is correct
38 Correct 123 ms 59976 KB Output is correct
39 Correct 131 ms 60232 KB Output is correct
40 Correct 112 ms 59484 KB Output is correct
41 Correct 76 ms 41032 KB Output is correct
42 Correct 68 ms 41216 KB Output is correct
43 Correct 105 ms 64840 KB Output is correct
44 Correct 7 ms 20316 KB Output is correct
45 Correct 7 ms 20304 KB Output is correct
46 Correct 7 ms 20472 KB Output is correct
47 Correct 7 ms 20304 KB Output is correct
48 Correct 10 ms 20228 KB Output is correct
49 Correct 9 ms 20816 KB Output is correct
50 Correct 10 ms 20816 KB Output is correct
51 Correct 12 ms 21068 KB Output is correct
52 Correct 12 ms 20816 KB Output is correct
53 Correct 95 ms 67912 KB Output is correct
54 Correct 123 ms 59980 KB Output is correct
55 Correct 123 ms 60232 KB Output is correct
56 Correct 115 ms 59728 KB Output is correct
57 Correct 170 ms 60232 KB Output is correct
58 Correct 114 ms 67912 KB Output is correct
59 Correct 104 ms 64840 KB Output is correct