답안 #338081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
338081 2020-12-22T12:40:29 Z Kevin_Zhang_TW Toll (BOI17_toll) C++17
100 / 100
171 ms 79468 KB
#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T>
bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
template<class T>
bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 50010, inf = 1e9, MAX_K = 5, MAX_L = 16;

int k, n, m, q;
#pragma GCC optimize("Ofast")
int dp[MAX_L][MAX_N][MAX_K][MAX_K];
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> k >> n >> m >> q;
	for (int i = 0;i < MAX_L;++i) for (int j = 0;j < n;++j) for (int l = 0;l < k;++l) for (int m = 0;m < k;++m) 
		dp[i][j][l][m] = inf;

	for (int a, b, t, i = 0;i < m;++i) {
		cin >> a >> b >> t;
		dp[0][a / k][a % k][b % k] = t;
	}
	for (int d = 1;1 << d <= n;++d) {
		for (int i = 0;i < n;++i)
			for (int j = 0;j < k;++j) for (int l = 0;l < k;++l)
				for (int m = 0;m < k;++m)
					chmin(dp[d][i][j][m], dp[d-1][i][j][l] + dp[d-1][i + (1<<(d-1))][l][m]);
	}
	auto qry = [&](int a, int b) {
		if (a / k == b / k) return -1;
		int dis = b / k - a / k;
		int *mat = new int[k], *tmp = new int[k];
		for (int i = 0;i < k;++i) mat[i] = inf;

		mat[a % k] = 0;

		for (int d = 0;1 << d <= dis;++d) if (dis >> d & 1) {
			DE(d);
			fill(tmp, tmp + k, inf);
			for (int i = 0;i < k;++i) for (int j = 0;j < k;++j) {
				chmin(tmp[j], mat[i] + dp[d][a / k][i][j]);
			}
			a += (1 << d) * k;
			swap(tmp, mat);
		}
		return mat[b % k] == inf ? -1 : mat[b % k];
	};
	while (q--) {
		int a, b;
		cin >> a >> b;
		cout << qry(a, b) << '\n';
	}
}

Compilation message

toll.cpp: In lambda function:
toll.cpp:18:17: warning: statement has no effect [-Wunused-value]
   18 | #define DE(...) 0
      |                 ^
toll.cpp:56:4: note: in expansion of macro 'DE'
   56 |    DE(d);
      |    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 79340 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 2 ms 2156 KB Output is correct
6 Correct 2 ms 2156 KB Output is correct
7 Correct 2 ms 2156 KB Output is correct
8 Correct 81 ms 79340 KB Output is correct
9 Correct 76 ms 79340 KB Output is correct
10 Correct 57 ms 79340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 79468 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 6 ms 2668 KB Output is correct
8 Correct 7 ms 2668 KB Output is correct
9 Correct 75 ms 79340 KB Output is correct
10 Correct 134 ms 79468 KB Output is correct
11 Correct 98 ms 79340 KB Output is correct
12 Correct 120 ms 79340 KB Output is correct
13 Correct 145 ms 47596 KB Output is correct
14 Correct 80 ms 47620 KB Output is correct
15 Correct 121 ms 47596 KB Output is correct
16 Correct 120 ms 47596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 2 ms 2028 KB Output is correct
7 Correct 2 ms 2028 KB Output is correct
8 Correct 4 ms 2028 KB Output is correct
9 Correct 3 ms 2028 KB Output is correct
10 Correct 67 ms 78572 KB Output is correct
11 Correct 90 ms 78700 KB Output is correct
12 Correct 126 ms 78572 KB Output is correct
13 Correct 135 ms 78572 KB Output is correct
14 Correct 121 ms 78696 KB Output is correct
15 Correct 117 ms 47468 KB Output is correct
16 Correct 121 ms 47468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 2 ms 2028 KB Output is correct
7 Correct 2 ms 2028 KB Output is correct
8 Correct 4 ms 2028 KB Output is correct
9 Correct 3 ms 2028 KB Output is correct
10 Correct 67 ms 78572 KB Output is correct
11 Correct 90 ms 78700 KB Output is correct
12 Correct 126 ms 78572 KB Output is correct
13 Correct 135 ms 78572 KB Output is correct
14 Correct 121 ms 78696 KB Output is correct
15 Correct 117 ms 47468 KB Output is correct
16 Correct 121 ms 47468 KB Output is correct
17 Correct 93 ms 78900 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 3 ms 2284 KB Output is correct
24 Correct 4 ms 2284 KB Output is correct
25 Correct 6 ms 2284 KB Output is correct
26 Correct 5 ms 2284 KB Output is correct
27 Correct 69 ms 78828 KB Output is correct
28 Correct 129 ms 78828 KB Output is correct
29 Correct 134 ms 78828 KB Output is correct
30 Correct 127 ms 78828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 79340 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 2 ms 2156 KB Output is correct
6 Correct 2 ms 2156 KB Output is correct
7 Correct 2 ms 2156 KB Output is correct
8 Correct 81 ms 79340 KB Output is correct
9 Correct 76 ms 79340 KB Output is correct
10 Correct 57 ms 79340 KB Output is correct
11 Correct 96 ms 79468 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 6 ms 2668 KB Output is correct
18 Correct 7 ms 2668 KB Output is correct
19 Correct 75 ms 79340 KB Output is correct
20 Correct 134 ms 79468 KB Output is correct
21 Correct 98 ms 79340 KB Output is correct
22 Correct 120 ms 79340 KB Output is correct
23 Correct 145 ms 47596 KB Output is correct
24 Correct 80 ms 47620 KB Output is correct
25 Correct 121 ms 47596 KB Output is correct
26 Correct 120 ms 47596 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 2 ms 2028 KB Output is correct
33 Correct 2 ms 2028 KB Output is correct
34 Correct 4 ms 2028 KB Output is correct
35 Correct 3 ms 2028 KB Output is correct
36 Correct 67 ms 78572 KB Output is correct
37 Correct 90 ms 78700 KB Output is correct
38 Correct 126 ms 78572 KB Output is correct
39 Correct 135 ms 78572 KB Output is correct
40 Correct 121 ms 78696 KB Output is correct
41 Correct 117 ms 47468 KB Output is correct
42 Correct 121 ms 47468 KB Output is correct
43 Correct 93 ms 78900 KB Output is correct
44 Correct 1 ms 492 KB Output is correct
45 Correct 1 ms 492 KB Output is correct
46 Correct 1 ms 492 KB Output is correct
47 Correct 1 ms 492 KB Output is correct
48 Correct 1 ms 492 KB Output is correct
49 Correct 3 ms 2284 KB Output is correct
50 Correct 4 ms 2284 KB Output is correct
51 Correct 6 ms 2284 KB Output is correct
52 Correct 5 ms 2284 KB Output is correct
53 Correct 69 ms 78828 KB Output is correct
54 Correct 129 ms 78828 KB Output is correct
55 Correct 134 ms 78828 KB Output is correct
56 Correct 127 ms 78828 KB Output is correct
57 Correct 171 ms 79328 KB Output is correct
58 Correct 75 ms 79340 KB Output is correct
59 Correct 106 ms 79340 KB Output is correct