답안 #788613

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
788613 2023-07-20T12:14:53 Z WLZ Toll (BOI17_toll) C++17
100 / 100
110 ms 16580 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
const int INF = 1e9;
template<typename T> inline istream& operator>>(istream &i, vector<T>& v) {
	for (int j = 0; j < (int) v.size(); j++) i >> v[j];
	return i;
}
template<typename T> inline ostream& operator<<(ostream &o, const vector<T>& v) {
	for (int i = 0; i < (int) v.size(); i++) o << v[i] << ' ';
   	o << '\n';
	return o;	
}

struct node {
	vector<vector<int>> mat;
	node* left, *right;
	int i, j;
};

vector<vector<int>> combine(const vector<vector<int>>& mat1, const vector<vector<int>>& mat2) {
	if ((int) mat1.size() == 0) return mat2;
	if ((int) mat2.size() == 0) return mat1;
	int V = (int) mat1.size();
	vector<vector<int>> newMat(V, vector<int>(V, INF));
	for (int k = 0; k < V; k++) {
		for (int i = 0; i < V; i++) {
			for (int j = 0; j < V; j++) {
				newMat[i][j] = min(newMat[i][j], mat1[i][k] + mat2[k][j]);
			}	
		}
	}
	return move(newMat);
}

int k, n, m, o;
vector<vector<ii>> g;

node *build(int l, int r) {
	//cout << l << ' ' << r << '\n';
	if (l == r) {
		vector<vector<int>> newMat(k, vector<int>(k, INF));
		for (int i = l * k; i < (l + 1) * k; i++) {
			for (auto& v : g[i]) {
				newMat[i % k][v.first % k] = v.second;
			}
		}
		#ifdef DEBUG
		cout << l << ' ' << r << ' ' << newMat;
		#endif
		//cout << newMat;
		return new node{newMat, nullptr, nullptr, l, r};
	}
	int mid = (l + r) / 2;
	node *left = build(l, mid);
	node *right = build(mid + 1, r);
	//cout << left->mat << right->mat;
	vector<vector<int>> newMat = combine(left->mat, right->mat);
	#ifdef DEBUG
	cout << l << ' ' << r << ' ' << newMat;
	#endif
	return new node{newMat, left, right, l, r};	
}

vector<vector<int>> query(node *cur, int i, int j) {
	if (cur->i > j || cur->j < i) return vector<vector<int>>(0);
	if (cur->i >= i && cur->j <= j) return cur->mat;
	return combine(query(cur->left, i, j), query(cur->right, i, j));
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);
	cin >> k >> n >> m >> o;
	//cout << k << n << m << o;
	g.resize(n);
	for (int i = 0; i < m; i++) {
		int a, b, t;
		cin >> a >> b >> t;
		g[a].emplace_back(b, t);
	}
	//cout << (n - 1) / k - 1;
	node* root = build(0, (n - 1) / k - 1);	
	while (o--) {
		int s, t;
		cin >> s >> t;
		int sLayer = s / k;
		int tLayer = t / k - 1;
		if (sLayer > tLayer) {
			cout << "-1\n";
			continue;
		}
		vector<vector<int>> mat = query(root, sLayer, tLayer);
		//cout << mat;
		int ans = mat[s % k][t % k];
		if (ans >= INF) ans = -1;
		cout << ans << '\n';
	}
	return 0;
}

Compilation message

toll.cpp: In function 'std::vector<std::vector<int> > combine(const std::vector<std::vector<int> >&, const std::vector<std::vector<int> >&)':
toll.cpp:34:13: warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
   34 |  return move(newMat);
      |         ~~~~^~~~~~~~
toll.cpp:34:13: note: remove 'std::move' call
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 16580 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 55 ms 16504 KB Output is correct
9 Correct 81 ms 16400 KB Output is correct
10 Correct 45 ms 14048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 14048 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 9 ms 724 KB Output is correct
8 Correct 11 ms 692 KB Output is correct
9 Correct 43 ms 16480 KB Output is correct
10 Correct 75 ms 13828 KB Output is correct
11 Correct 62 ms 14152 KB Output is correct
12 Correct 51 ms 12080 KB Output is correct
13 Correct 51 ms 10008 KB Output is correct
14 Correct 33 ms 8580 KB Output is correct
15 Correct 31 ms 7500 KB Output is correct
16 Correct 31 ms 7492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 30 ms 16408 KB Output is correct
11 Correct 33 ms 13900 KB Output is correct
12 Correct 42 ms 13652 KB Output is correct
13 Correct 46 ms 14156 KB Output is correct
14 Correct 37 ms 12936 KB Output is correct
15 Correct 23 ms 7476 KB Output is correct
16 Correct 23 ms 7468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 30 ms 16408 KB Output is correct
11 Correct 33 ms 13900 KB Output is correct
12 Correct 42 ms 13652 KB Output is correct
13 Correct 46 ms 14156 KB Output is correct
14 Correct 37 ms 12936 KB Output is correct
15 Correct 23 ms 7476 KB Output is correct
16 Correct 23 ms 7468 KB Output is correct
17 Correct 44 ms 13948 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 312 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 5 ms 596 KB Output is correct
24 Correct 6 ms 596 KB Output is correct
25 Correct 10 ms 636 KB Output is correct
26 Correct 9 ms 596 KB Output is correct
27 Correct 41 ms 16396 KB Output is correct
28 Correct 55 ms 13784 KB Output is correct
29 Correct 70 ms 14268 KB Output is correct
30 Correct 46 ms 13004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 16580 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 55 ms 16504 KB Output is correct
9 Correct 81 ms 16400 KB Output is correct
10 Correct 45 ms 14048 KB Output is correct
11 Correct 49 ms 14048 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 9 ms 724 KB Output is correct
18 Correct 11 ms 692 KB Output is correct
19 Correct 43 ms 16480 KB Output is correct
20 Correct 75 ms 13828 KB Output is correct
21 Correct 62 ms 14152 KB Output is correct
22 Correct 51 ms 12080 KB Output is correct
23 Correct 51 ms 10008 KB Output is correct
24 Correct 33 ms 8580 KB Output is correct
25 Correct 31 ms 7500 KB Output is correct
26 Correct 31 ms 7492 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 320 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 30 ms 16408 KB Output is correct
37 Correct 33 ms 13900 KB Output is correct
38 Correct 42 ms 13652 KB Output is correct
39 Correct 46 ms 14156 KB Output is correct
40 Correct 37 ms 12936 KB Output is correct
41 Correct 23 ms 7476 KB Output is correct
42 Correct 23 ms 7468 KB Output is correct
43 Correct 44 ms 13948 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 0 ms 312 KB Output is correct
47 Correct 1 ms 324 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 5 ms 596 KB Output is correct
50 Correct 6 ms 596 KB Output is correct
51 Correct 10 ms 636 KB Output is correct
52 Correct 9 ms 596 KB Output is correct
53 Correct 41 ms 16396 KB Output is correct
54 Correct 55 ms 13784 KB Output is correct
55 Correct 70 ms 14268 KB Output is correct
56 Correct 46 ms 13004 KB Output is correct
57 Correct 110 ms 14688 KB Output is correct
58 Correct 54 ms 16452 KB Output is correct
59 Correct 64 ms 14144 KB Output is correct