Submission #651524

# Submission time Handle Problem Language Result Execution time Memory
651524 2022-10-19T08:12:31 Z ghostwriter Toll (BOI17_toll) C++14
0 / 100
42 ms 7628 KB
#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 = 105;
const int Dxk = 555;
int n, m, k, o, sb[D], eb[D], d[D][Dxk][Dxk], 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]) {
            assert(j - sb[i] < Dxk);
			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]) {
			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 - sb[i]][x] = min(d[i][j - sb[i]][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[j][v - sb[i + 1]] = t;
            }
        }
    	FOR(j, i + 1, n / k / D) {
            EACH(z, q1[i][j]) {
                int a = q[z].st, b = q[z].nd;
                FRN(x1, 5) {
                    int x = eb[i] - x1;
                    if (x < sb[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 - sb[i]][x - eb[i]] + d1[x1][y1] + 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;
                if (x < sb[i]) continue;
                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 <= eb[j]) continue;
                            d2[x1][y - sb[j + 1]] = min(1LL * d2[x1][y - sb[j + 1]], 1LL * d1[x1][x2] + d[j][x2][b - sb[j]] + t);
                        }
                    }
                }
            }
            swap(d1, d2);
    	}
    }
    FOR(i, 1, o) cout << (ans[i] < oo? ans[i] : -1) << '\n';
    return 0;
}
/*
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
----------------------------------------------------------------
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 time Memory Grader output
1 Runtime error 36 ms 7628 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 42 ms 6896 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1748 KB Output is correct
2 Correct 1 ms 1748 KB Output is correct
3 Correct 1 ms 1748 KB Output is correct
4 Correct 2 ms 1748 KB Output is correct
5 Correct 2 ms 1748 KB Output is correct
6 Incorrect 3 ms 4052 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1748 KB Output is correct
2 Correct 1 ms 1748 KB Output is correct
3 Correct 1 ms 1748 KB Output is correct
4 Correct 2 ms 1748 KB Output is correct
5 Correct 2 ms 1748 KB Output is correct
6 Incorrect 3 ms 4052 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 36 ms 7628 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -