답안 #643633

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
643633 2022-09-22T16:54:02 Z thiago_bastos Toll (BOI17_toll) C++17
100 / 100
84 ms 78060 KB
#include "bits/stdc++.h"

using namespace std;

#define INF 1000000000
#define INFLL 1000000000000000000ll
#define EPS 1e-9
#define all(x) x.begin(),x.end()
#define pb push_back
#define fi first
#define sc second

using i64 = long long;
using u64 = unsigned long long;
using ld = long double;
using ii = pair<int, int>;

const int N = 5e4 + 10, K = 5, L = 16;

int adj[N][K][K], n, r, m, o, cost[L][N][K][K];

void min_path(int c[][K], int a[][K], int b[][K]) {
	int d[K][K];

	for(int i = 0; i < r; ++i)
	for(int j = 0; j < r; ++j)
		d[i][j] = INF;
	
	for(int k = 0; k < r; ++k)
		for(int i = 0; i < r; ++i)
			for(int j = 0; j < r; ++j)
				d[i][j] = min(d[i][j], a[i][k] + b[k][j]);

	for(int i = 0; i < r; ++i)
	for(int j = 0; j < r; ++j)
		c[i][j] = d[i][j];
}

void query(int a[][K], int s, int t) {
	for(int i = L - 1; i >= 0; --i) {
		if(s + (1 << i) <= t) {
			min_path(a, a, cost[i][s]);
			s += 1 << i;
		}
	}
}

void solve() {
	cin >> r >> n >> m >> o;

	n = (n - 1) / r + 1;

	for(int i = 0; i < n; ++i)
	for(int j = 0; j < r; ++j)
	for(int k = 0; k < r; ++k)
		adj[i][j][k] = INF;

	for(int i = 0; i < m; ++i) {
		int a, b, t;
		cin >> a >> b >> t;
		adj[a / r][a % r][b % r] = t;
	}

	for(int i = 0; i < n; ++i)
	for(int j = 0; j < r; ++j)
	for(int k = 0; k < r; ++k)
		cost[0][i][j][k] = adj[i][j][k];

	for(int i = 1; (1 << i) < n; ++i)
		for(int j = 0; j + (1 << i) < n; ++j)
			min_path(cost[i][j], cost[i - 1][j], cost[i - 1][j + (1 << (i - 1))]);

	for(int i = 0; i < o; ++i) {
		int a, b, A[K][K];

		cin >> a >> b;

		if(a / r == b / r) {
			cout << "-1\n";
			continue;
		}

		for(int k = 0; k < r; ++k)
			for(int j = 0; j < r; ++j)
				A[k][j] = k == a % r ? adj[a / r][k][j] : INF;
		
		query(A, a / r + 1, b / r);

		cout << (A[a % r][b % r] == INF ? -1 : A[a % r][b % r]) << '\n';
	}
}

int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 77132 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 1364 KB Output is correct
6 Correct 2 ms 1364 KB Output is correct
7 Correct 1 ms 1364 KB Output is correct
8 Correct 62 ms 77828 KB Output is correct
9 Correct 58 ms 77632 KB Output is correct
10 Correct 50 ms 77236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 36428 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 4 ms 1364 KB Output is correct
8 Correct 5 ms 980 KB Output is correct
9 Correct 53 ms 77944 KB Output is correct
10 Correct 58 ms 25628 KB Output is correct
11 Correct 51 ms 38092 KB Output is correct
12 Correct 43 ms 24524 KB Output is correct
13 Correct 55 ms 10188 KB Output is correct
14 Correct 33 ms 14924 KB Output is correct
15 Correct 37 ms 8960 KB Output is correct
16 Correct 39 ms 8968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 56 ms 77848 KB Output is correct
11 Correct 44 ms 37852 KB Output is correct
12 Correct 49 ms 25432 KB Output is correct
13 Correct 62 ms 25676 KB Output is correct
14 Correct 56 ms 25036 KB Output is correct
15 Correct 35 ms 8904 KB Output is correct
16 Correct 31 ms 8964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 1364 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 56 ms 77848 KB Output is correct
11 Correct 44 ms 37852 KB Output is correct
12 Correct 49 ms 25432 KB Output is correct
13 Correct 62 ms 25676 KB Output is correct
14 Correct 56 ms 25036 KB Output is correct
15 Correct 35 ms 8904 KB Output is correct
16 Correct 31 ms 8964 KB Output is correct
17 Correct 45 ms 37960 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 2 ms 1364 KB Output is correct
24 Correct 2 ms 852 KB Output is correct
25 Correct 4 ms 596 KB Output is correct
26 Correct 3 ms 600 KB Output is correct
27 Correct 54 ms 77912 KB Output is correct
28 Correct 75 ms 25488 KB Output is correct
29 Correct 56 ms 25676 KB Output is correct
30 Correct 50 ms 25112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 77132 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 1364 KB Output is correct
6 Correct 2 ms 1364 KB Output is correct
7 Correct 1 ms 1364 KB Output is correct
8 Correct 62 ms 77828 KB Output is correct
9 Correct 58 ms 77632 KB Output is correct
10 Correct 50 ms 77236 KB Output is correct
11 Correct 50 ms 36428 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 4 ms 1364 KB Output is correct
18 Correct 5 ms 980 KB Output is correct
19 Correct 53 ms 77944 KB Output is correct
20 Correct 58 ms 25628 KB Output is correct
21 Correct 51 ms 38092 KB Output is correct
22 Correct 43 ms 24524 KB Output is correct
23 Correct 55 ms 10188 KB Output is correct
24 Correct 33 ms 14924 KB Output is correct
25 Correct 37 ms 8960 KB Output is correct
26 Correct 39 ms 8968 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 1364 KB Output is correct
33 Correct 1 ms 852 KB Output is correct
34 Correct 2 ms 592 KB Output is correct
35 Correct 2 ms 596 KB Output is correct
36 Correct 56 ms 77848 KB Output is correct
37 Correct 44 ms 37852 KB Output is correct
38 Correct 49 ms 25432 KB Output is correct
39 Correct 62 ms 25676 KB Output is correct
40 Correct 56 ms 25036 KB Output is correct
41 Correct 35 ms 8904 KB Output is correct
42 Correct 31 ms 8964 KB Output is correct
43 Correct 45 ms 37960 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 2 ms 1364 KB Output is correct
50 Correct 2 ms 852 KB Output is correct
51 Correct 4 ms 596 KB Output is correct
52 Correct 3 ms 600 KB Output is correct
53 Correct 54 ms 77912 KB Output is correct
54 Correct 75 ms 25488 KB Output is correct
55 Correct 56 ms 25676 KB Output is correct
56 Correct 50 ms 25112 KB Output is correct
57 Correct 84 ms 20520 KB Output is correct
58 Correct 60 ms 78060 KB Output is correct
59 Correct 56 ms 38196 KB Output is correct