Submission #562960

# Submission time Handle Problem Language Result Execution time Memory
562960 2022-05-15T18:59:45 Z Bungmint Toll (BOI17_toll) C++17
100 / 100
141 ms 13724 KB
// Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound

constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
const ld PI = acos((ld)-1.0);
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
constexpr bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
constexpr bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
	return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
	os << '{';
	string sep;
	for (const T &x : v)
		os << sep << x, sep = ", ";
	return os << '}';
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &v) {
	os << vector<T>(all(v));
	return os;
}
template <typename T, typename S, typename C>
ostream &operator<<(ostream &os, priority_queue<T, S, C> pq) {
	vector<T> v;
	while (sz(pq)) {
		v.pb(pq.top());
		pq.pop();
	}
	os << v;
	return os;
}
void dbg_out()
{
	cerr << "\033[0m" << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
	cerr << ' ' << H;
	dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "\033[1;35m(" << #__VA_ARGS__ << "):\033[33m", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif

inline namespace RecursiveLambda
{
	template <typename Fun>
	struct y_combinator_result
	{
		Fun fun_;
		template <typename T>
		explicit y_combinator_result(T &&fun) : fun_(forward<T>(fun)) {}
		template <typename... Args>
		decltype(auto) operator()(Args &&...args)
		{
			return fun_(ref(*this), forward<Args>(args)...);
		}
	};
	template <typename Fun>
	decltype(auto) y_combinator(Fun &&fun)
	{
		return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
	}
};

inline namespace Range {
	class ForwardRange {
		int src, dst;

	  public:
	  	explicit constexpr ForwardRange(const int l, const int r) : src(l), dst(r) {}
		explicit constexpr ForwardRange(const int n) : src(0), dst(n) {}
		constexpr ForwardRange begin() const { return *this; }
		constexpr monostate end() const { return {}; }
		constexpr bool operator!=(monostate) const { return src < dst; }
		constexpr void operator++() const {}
		constexpr int operator*() { return src++; }
	};
	class BackwardRange {
		int src, dst;

	  public:
	  	explicit constexpr BackwardRange(const int l, const int r) : src(r), dst(l) {}
		explicit constexpr BackwardRange(const int n) : src(n), dst(0) {}
		constexpr BackwardRange begin() const { return *this; }
		constexpr monostate end() const { return {}; }
		constexpr bool operator!=(monostate) const { return src > dst; }
		constexpr void operator++() const {}
		constexpr int operator*() { return --src; }
	};
	using rep = ForwardRange;
	using per = BackwardRange;
};

int K, N, MM, O;
vpi adj[51'100], radj[51'000];
int A[11'000], B[11'000], ans[11'000];
int lef[51'000][5], rig[51'000][5];

void DnC(int L, int R, vi& query) {
	if (sz(query) == 0) return;
	if (L + 1 == R) {
		for (int &id : query) {
			ans[id] = (A[id] == B[id] ? 0 : -1);
		}
		return;
	}
	vi lefq, rigq;
	int M = (L + R) / 2;
	for (int i : rep(K)) {
		int cur = M * K + i;
		for (int j : rep(K)) {
			if (i == j) lef[cur][j] = rig[cur][j] = 0;
			else lef[cur][j] = rig[cur][j] = INF;
		}
	}
	for (int i : per(L, M)) {
		for (int j : rep(K)) {
			int cur = i * K + j;
			for (int jj : rep(K)) {
				lef[cur][jj] = INF;
				for (auto &[v, t] : adj[cur]) {
					ckmin(lef[cur][jj], t + lef[v][jj]);
				}	
			}
		}
	}
	for (int i : rep(M + 1, R)) {
		for (int j : rep(K)) {
			int cur = i * K + j;
			for (int jj : rep(K)) {
				rig[cur][jj] = (INF);
				for (auto &[v, t] : radj[cur]) {
					ckmin(rig[cur][jj], t + rig[v][jj]);
				}	
			}
		}
	}
	for (auto &id : query) {
		if (B[id] < K * M) lefq.pb(id);
		else if (A[id] >= M * K) rigq.pb(id);
		else{
			ans[id] = INF;
			dbg(L, R, id);
			for (int i : rep(K)) {
				ckmin(ans[id], lef[A[id]][i] + rig[B[id]][i]);
			}
			if (ans[id] == INF) ans[id] = -1;
		}
	}
	DnC(L, M, lefq), DnC(M, R, rigq);
}

void solve()
{
	cin >> K >> N >> MM >> O;
	vi query(O);
	for (int i : rep(MM)) {
		int a, b, t;
		cin >> a >> b >> t;
		adj[a].pb({b, t});
		radj[b].pb({a, t});
	}
	for (int i : rep(O)) cin >> A[i] >> B[i], query[i] = i;
	DnC(0, (N + K - 1) / K + 10, query);
	for (int i : rep(O)) cout << ans[i] << '\n';
}

int main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
	{
		solve();
	}
#ifdef LOCAL
	cerr << "Time elapsed: " << 1.0 * (double)clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}

Compilation message

toll.cpp: In function 'void DnC(int, int, vi&)':
toll.cpp:80:18: warning: statement has no effect [-Wunused-value]
   80 | #define dbg(...) 42
      |                  ^~
toll.cpp:182:4: note: in expansion of macro 'dbg'
  182 |    dbg(L, R, id);
      |    ^~~
toll.cpp: In function 'void solve()':
toll.cpp:196:11: warning: unused variable 'i' [-Wunused-variable]
  196 |  for (int i : rep(MM)) {
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 42 ms 8032 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 3 ms 2772 KB Output is correct
7 Correct 3 ms 2876 KB Output is correct
8 Correct 31 ms 8044 KB Output is correct
9 Correct 27 ms 7864 KB Output is correct
10 Correct 5 ms 4820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 7496 KB Output is correct
2 Correct 2 ms 2728 KB Output is correct
3 Correct 2 ms 2728 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 5 ms 3156 KB Output is correct
8 Correct 5 ms 3132 KB Output is correct
9 Correct 30 ms 8404 KB Output is correct
10 Correct 64 ms 11576 KB Output is correct
11 Correct 69 ms 9252 KB Output is correct
12 Correct 38 ms 8652 KB Output is correct
13 Correct 71 ms 11424 KB Output is correct
14 Correct 38 ms 8396 KB Output is correct
15 Correct 41 ms 7444 KB Output is correct
16 Correct 46 ms 7440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 25 ms 7728 KB Output is correct
11 Correct 51 ms 7628 KB Output is correct
12 Correct 93 ms 9420 KB Output is correct
13 Correct 91 ms 9996 KB Output is correct
14 Correct 53 ms 8656 KB Output is correct
15 Correct 41 ms 6396 KB Output is correct
16 Correct 41 ms 6440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 25 ms 7728 KB Output is correct
11 Correct 51 ms 7628 KB Output is correct
12 Correct 93 ms 9420 KB Output is correct
13 Correct 91 ms 9996 KB Output is correct
14 Correct 53 ms 8656 KB Output is correct
15 Correct 41 ms 6396 KB Output is correct
16 Correct 41 ms 6440 KB Output is correct
17 Correct 52 ms 7748 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2740 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 3 ms 2900 KB Output is correct
24 Correct 3 ms 2904 KB Output is correct
25 Correct 4 ms 2900 KB Output is correct
26 Correct 4 ms 2900 KB Output is correct
27 Correct 23 ms 7756 KB Output is correct
28 Correct 70 ms 9668 KB Output is correct
29 Correct 103 ms 10028 KB Output is correct
30 Correct 75 ms 8872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 8032 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 3 ms 2772 KB Output is correct
7 Correct 3 ms 2876 KB Output is correct
8 Correct 31 ms 8044 KB Output is correct
9 Correct 27 ms 7864 KB Output is correct
10 Correct 5 ms 4820 KB Output is correct
11 Correct 56 ms 7496 KB Output is correct
12 Correct 2 ms 2728 KB Output is correct
13 Correct 2 ms 2728 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 5 ms 3156 KB Output is correct
18 Correct 5 ms 3132 KB Output is correct
19 Correct 30 ms 8404 KB Output is correct
20 Correct 64 ms 11576 KB Output is correct
21 Correct 69 ms 9252 KB Output is correct
22 Correct 38 ms 8652 KB Output is correct
23 Correct 71 ms 11424 KB Output is correct
24 Correct 38 ms 8396 KB Output is correct
25 Correct 41 ms 7444 KB Output is correct
26 Correct 46 ms 7440 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 3 ms 2772 KB Output is correct
34 Correct 3 ms 2900 KB Output is correct
35 Correct 3 ms 2900 KB Output is correct
36 Correct 25 ms 7728 KB Output is correct
37 Correct 51 ms 7628 KB Output is correct
38 Correct 93 ms 9420 KB Output is correct
39 Correct 91 ms 9996 KB Output is correct
40 Correct 53 ms 8656 KB Output is correct
41 Correct 41 ms 6396 KB Output is correct
42 Correct 41 ms 6440 KB Output is correct
43 Correct 52 ms 7748 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2740 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 3 ms 2900 KB Output is correct
50 Correct 3 ms 2904 KB Output is correct
51 Correct 4 ms 2900 KB Output is correct
52 Correct 4 ms 2900 KB Output is correct
53 Correct 23 ms 7756 KB Output is correct
54 Correct 70 ms 9668 KB Output is correct
55 Correct 103 ms 10028 KB Output is correct
56 Correct 75 ms 8872 KB Output is correct
57 Correct 141 ms 13724 KB Output is correct
58 Correct 26 ms 8940 KB Output is correct
59 Correct 56 ms 9764 KB Output is correct