Submission #986009

# Submission time Handle Problem Language Result Execution time Memory
986009 2024-05-19T15:40:26 Z tfgs Toll (BOI17_toll) C++17
17 / 100
54 ms 21760 KB
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;

#ifdef LOCAL
#include "algo/debug.h"
#endif

#define f first
#define s second
template<class T> using V = vector<T>; 
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) 
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }

// dist_to(src class in m, dest layer, dest layer class)
// dist_from(src layer, src class, dest class in m)
const int max_k = 5, max_n = 5e4;
int k, n, m, q, dist_to[max_k][max_n][max_k], dist_from[max_n][max_k][max_k];
const int max_q = 10000;
int ans[max_q];
array<int, 2> queries[max_q];
V<array<int, 2>> g[max_n][max_k];

void dnc(int l, int r, vi query_inds) {
    if (l == r-1) {
        for (int ind : query_inds) {
            auto [l, r] = queries[ind];
            assert(l/k == r/k);
            ans[ind] = l == r ? 0 : -1;
        }
        return;
    }

    int m = (l+r)/2;
    {
        for (int from = 0; from < k; from++) {
            for (int i = m; i < r; i++) {
                fill(all(dist_to[from][i]), inf);
            }
        }
        for (int from = 0; from < k; from++) {
            dist_to[from][m][from] = 0;
            for (int i = m; i < r-1; i++) {
                for (int now = 0; now < k; now++) {
                    for (auto [w, nxt] : g[i][now]) {
                        dist_to[from][i+1][nxt] = min(dist_to[from][i+1][nxt], dist_to[from][i][now]+w);
                    }
                }
            }
        }
    }
    {
        for (int src_layer = l; src_layer <= m; src_layer++) {
            for (int src_class = 0; src_class < k; src_class++) {
                fill(all(dist_from[src_layer][src_class]), inf);
            }
        }
        for (int dest_class = 0; dest_class < k; dest_class++) {
            dist_from[m][dest_class][dest_class] = 0;
            for (int src_layer = m-1; src_layer >= l; src_layer--) {
                for (int src_class = 0; src_class < k; src_class++) {
                    for (auto [w, nxt_class] : g[src_layer][src_class]) {
                        dist_from[src_layer][src_class][dest_class] = min(
                            dist_from[src_layer][src_class][dest_class],
                            w+dist_from[src_layer+1][nxt_class][dest_class]
                        );
                    }
                }
            }
        }
    }

    vi todo[2];
    for (int ind : query_inds) {
        auto [l, r] = queries[ind];
        if (l/k <= m && m <= r/k) {
            int res = inf;
            for (int m_class = 0; m_class < k; m_class++) {
                res = min(res, dist_from[l/k][l%k][m_class]+dist_to[m_class][r/k][r%k]);
            }
            if (res == inf) res = -1;
            ans[ind] = res;
        } else {
            todo[queries[ind][0] >= m].pb(ind);
        }
    }
    dnc(l, m, todo[0]);
    dnc(m, r, todo[1]);
}

void solve() {
    cin >> k >> n >> m >> q; 
    for (int i = 0; i < m; i++) {
        int u, v, w; cin >> u >> v >> w;
        g[u/k][u%k].pb({w, v%k});
    }

    for (int i = 0; i < q; i++) {
        cin >> queries[i][0] >> queries[i][1];
    }
    vi query_inds(q); iota(all(query_inds), 0);

    dnc(0, (n-1)/k+1, query_inds);
    for (int i = 0; i < q; i++) cout << ans[i] << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 14652 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 3 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 3 ms 9048 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 23 ms 14428 KB Output is correct
9 Correct 21 ms 14428 KB Output is correct
10 Correct 8 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 14676 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 3 ms 8796 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 22 ms 15452 KB Output is correct
10 Correct 50 ms 17756 KB Output is correct
11 Correct 34 ms 16212 KB Output is correct
12 Correct 35 ms 15800 KB Output is correct
13 Correct 54 ms 18016 KB Output is correct
14 Correct 31 ms 16080 KB Output is correct
15 Correct 33 ms 15444 KB Output is correct
16 Correct 34 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Runtime error 10 ms 21760 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Runtime error 10 ms 21760 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 14652 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 3 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 3 ms 9048 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 23 ms 14428 KB Output is correct
9 Correct 21 ms 14428 KB Output is correct
10 Correct 8 ms 12892 KB Output is correct
11 Correct 35 ms 14676 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 4 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 22 ms 15452 KB Output is correct
20 Correct 50 ms 17756 KB Output is correct
21 Correct 34 ms 16212 KB Output is correct
22 Correct 35 ms 15800 KB Output is correct
23 Correct 54 ms 18016 KB Output is correct
24 Correct 31 ms 16080 KB Output is correct
25 Correct 33 ms 15444 KB Output is correct
26 Correct 34 ms 15452 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
29 Runtime error 10 ms 21760 KB Execution killed with signal 6
30 Halted 0 ms 0 KB -