Submission #651505

#TimeUsernameProblemLanguageResultExecution timeMemory
651505ghostwriterToll (BOI17_toll)C++14
0 / 100
73 ms54528 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #else #define debug(...) #endif #define ft front #define bk back #define st first #define nd second #define ins insert #define ers erase #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define bg begin #define ed end #define all(x) x.bg(), x.ed() #define sz(x) (int)x.size() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define FOR(i, l, r) for (int i = l; i <= r; ++i) #define FOS(i, r, l) for (int i = r; i >= l; --i) #define FRN(i, n) for (int i = 0; i < n; ++i) #define FSN(i, n) for (int i = n - 1; i >= 0; --i) #define EACH(i, x) for (auto &i : x) #define WHILE while #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* ---------------------------------------------------------------- END OF TEMPLATE ---------------------------------------------------------------- Tran The Bao - ghostwriter Training for VOI23 gold medal ---------------------------------------------------------------- GOAT ---------------------------------------------------------------- */ const int oo = 1e9 + 5; const int N = 5e4 + 5; const int D = 225; int n, m, k, o, sb[D], eb[D], d[D][D][D], d1[5][5], d2[5][5], ans[N]; pi q[N]; vi q1[D][D]; vpi adj[N]; signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); cin >> k >> n >> m >> o; FOR(i, 1, m) { int a, b, t; cin >> a >> b >> t; adj[a].pb({b, t}); } FOR(i, 1, o) { cin >> q[i].st >> q[i].nd; q1[q[i].st / k / D][q[i].nd / k / D].pb(i); ans[i] = oo; } memset(sb, -1, sizeof sb); memset(eb, -1, sizeof eb); FRN(i, n) { if (sb[i / k / D] == -1) sb[i / k / D] = i; eb[i / k / D] = i; } FOR(i, 0, n / k / D) { FOR(j, sb[i], eb[i]) { FOR(z, 0, eb[i] - sb[i]) d[i][j - sb[i]][z] = oo; d[i][j - sb[i]][j - sb[i]] = 0; } FOS(j, eb[i], sb[i]) { EACH(z, adj[j + sb[i]]) { int v = z.st - sb[i], t = z.nd; if (v + sb[i] > eb[i]) continue; FOR(x, 0, eb[i] - sb[i]) d[i][j][x] = min(d[i][j][x], d[i][v][x] + t); } } } FOR(i, 0, n / k / D) { EACH(j, q1[i][i]) ans[j] = d[i][q[j].st - sb[i]][q[j].nd - sb[i]]; if (i == n / k / D) continue; FRN(j, 5) FRN(z, 5) d1[j][z] = oo; FRN(j, 5) { int x = eb[i] - j; if (x < sb[i]) continue; EACH(z, adj[x]) { int v = z.st, t = z.nd; if (v <= eb[i]) continue; d1[x - sb[i]][v - sb[i + 1]] = t; } } FOR(j, i + 1, n / k / D) { EACH(z, q1[i][i + 1]) { int a = q[z].st, b = q[z].nd; FRN(x1, 5) { int x = eb[i] - x1; if (x < eb[i]) continue; FRN(y1, 5) { int y = sb[j] + y1; if (y > eb[j]) continue; ans[z] = min(1LL * ans[z], 1LL * d[i][a][x - eb[i]] + d1[x][y] + d[j][y - sb[j]][b - sb[j]]); } } } if (j == n / k / D) continue; FRN(x1, 5) FRN(x2, 5) d2[x1][x2] = oo; FRN(x1, 5) { int x = eb[i] - x1; FRN(x2, 5) { int a = sb[j] + x2; if (a > eb[j]) continue; FRN(y2, 5) { int b = eb[j] - y2; if (b < sb[j]) continue; EACH(y1, adj[b]) { int y = y1.st, t = y1.nd; if (y <= sb[j]) continue; d2[x][y] = min(1LL * d2[x][y], 1LL * d1[x - sb[i]][a - sb[j]] + d[j][a - sb[j]][b - sb[j]] + t); } } } } swap(d1, d2); } } FOR(i, 1, o) cout << (ans[i] < oo? ans[i] : -1) << '\n'; return 0; } /* ---------------------------------------------------------------- From Benq: stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH ---------------------------------------------------------------- */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...