답안 #567581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
567581 2022-05-23T17:50:40 Z _karan_gandhi Toll (BOI17_toll) C++17
7 / 100
170 ms 198824 KB
#include <bits/stdc++.h>
using namespace std;

#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long

void solve() {
	ll int k, n, m, o; cin >> k >> n >> m >> o;

	const ll int LOG = 20;
	// dp[i][i + 2 ^ k][x][y];
	ll int dp[n / k][LOG][5][5];
	memset(dp, 1, sizeof dp);

	vector<vector<pair<ll int, ll int>>> adj(n);

	for (ll int i = 0; i < m; i++) {
		ll int a, b; cin >> a >> b;
		ll int c; cin >> c;

		adj[a].emplace_back(b, c);
	}

	for (ll int i = 0; i < n; i++) {
		for (auto [v, wt] : adj[i]) {
			dp[i / k][0][i % k][v % k] = wt;
		}
	}

	auto combine = [&](ll int target[5][5], ll int a[5][5], ll int b[5][5]) {
		for (ll int i = 0; i < k; i++) {
			for (ll int j = 0; j < k; j++) {
				for (ll int l = 0; l < k; l++) {
					target[i][j] = min(target[i][j], a[i][l] + b[l][j]);
				}
			}
		}
	};

	for (ll int i = 1; i < LOG; i++) {
		for (ll int j = 0; (j + (1 << i)) < (n + k - 1) / k; j++) {
			combine(dp[j][i], dp[j][i - 1], dp[j + (1 << (i - 1))][i - 1]);
		}
	}

	auto query = [&](ll int start, ll int levels, ll int en) {
		ll int res[5][5];
		ll int level = start / k;

		memset(res, 1, sizeof res);

		for (ll int i = 0; i < k; i++) res[i][i] = 0;
		ll int pow = LOG;

		while (levels > 0) {
			while (levels < (1 << pow)) pow--;

			ll int new_res[5][5];
			memset(new_res, 1, sizeof new_res);

			combine(new_res, res, dp[level][pow]);
			memcpy(res, new_res, sizeof res);

			level += (1 << pow);
			levels -= (1 << pow);
		}

		return res[start % k][en % k];
	};

	while (o--) {
		ll int a, b; cin >> a >> b;
		ll int levels = abs(a / k - b / k);

		if (levels == 0) {
			cout << -1 << endl;
		}

		ll int res = query(a, levels, b);

		if (res > 1e16) {
			cout << -1 << endl;
		} else {
			cout << res << endl;
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	ll int T = 1;
	// cin >> T;
	while (T--)
		solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 198824 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 3 ms 4180 KB Output is correct
6 Correct 3 ms 4172 KB Output is correct
7 Correct 3 ms 4180 KB Output is correct
8 Correct 137 ms 198708 KB Output is correct
9 Correct 138 ms 198656 KB Output is correct
10 Correct 141 ms 197160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 101852 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 198824 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 3 ms 4180 KB Output is correct
6 Correct 3 ms 4172 KB Output is correct
7 Correct 3 ms 4180 KB Output is correct
8 Correct 137 ms 198708 KB Output is correct
9 Correct 138 ms 198656 KB Output is correct
10 Correct 141 ms 197160 KB Output is correct
11 Correct 118 ms 101852 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Incorrect 0 ms 212 KB Output isn't correct
14 Halted 0 ms 0 KB -