Submission #380415

# Submission time Handle Problem Language Result Execution time Memory
380415 2021-03-21T16:17:33 Z rqi Toll (BOI17_toll) C++14
100 / 100
687 ms 54240 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;

#define mp make_pair
#define pb push_back
#define f first
#define s second

#define sz(x) (int)(x).size()

void ckmin(int& a, int b){
    a = min(a, b);
}

const int MOD = 1e9+7;

const int mx = 50005;

int K, N, M, O;

vpi adj[mx];
vpi radj[mx];

pair<vi, vi> mid_dists[1<<17][5];

void genDists(int L, int R, int ind = 1){
    if(L > R) return;
    int M = (L+R)/2;

    //cout << "L, M, R: " << L << ", " << M << ", " << R << "\n";

    for(int rem = 0; rem < K; rem++){ //gen distances from M*K+rem
        //cout << "rem: " << rem << "\n";

        {
        
        mid_dists[ind][rem].f = vi((M-L+1)*K, MOD);
        vi& v = mid_dists[ind][rem].f;
        priority_queue<pi, vector<pi>, greater<pi>> pq;
        v[M*K+rem-L*K] = 0;
        pq.push(mp(v[M*K+rem-L*K], M*K+rem));
        while(sz(pq)){
            int node = pq.top().s; pq.pop();
            for(auto u: radj[node]){
                if(u.f < L*K) continue;
                if(v[u.f-L*K] <= v[node-L*K]+u.s){
                    continue;
                }
                v[u.f-L*K] = v[node-L*K]+u.s;
                pq.push(mp(v[u.f-L*K], u.f));
            }
        }

        }

        {

        mid_dists[ind][rem].s = vi((R-M+1)*K, MOD);
        vi& v = mid_dists[ind][rem].s;
        priority_queue<pi, vector<pi>, greater<pi>> pq;
        v[M*K+rem-M*K] = 0;
        pq.push(mp(v[M*K+rem-M*K], M*K+rem));
        while(sz(pq)){
            int node = pq.top().s; pq.pop();
            for(auto u: adj[node]){
                if(u.f >= (R+1)*K) continue;
                if(v[u.f-M*K] <= v[node-M*K]+u.s){
                    continue;
                }
                v[u.f-M*K] = v[node-M*K]+u.s;
                pq.push(mp(v[u.f-M*K], u.f));
            }
        }

        }

        // cout << "mid_dists[ind][rem].f:" << "\n";
        // for(int i = 0; i < sz(mid_dists[ind][rem].f); i++){
        //     cout << i << " " << mid_dists[ind][rem].f[i] << "\n";
        // }
        // cout << "mid_dists[ind][rem].s:" << "\n";
        // for(int i = 0; i < sz(mid_dists[ind][rem].s); i++){
        //     cout << i << " " << mid_dists[ind][rem].s[i] << "\n";
        // }
    }

    genDists(L, M-1, 2*ind);
    genDists(M+1, R, 2*ind+1);
}

int getDist(int a, int b, int L, int R, int ind = 1){
    int M = (L+R)/2;

    if(b/K < M){
        return getDist(a, b, L, M-1, 2*ind);
    }
    else if(M < a/K){
        return getDist(a, b, M+1, R, 2*ind+1);
    }

    int res = MOD;
    for(int rem = 0; rem < K; rem++){
        ckmin(res, mid_dists[ind][rem].f[a-L*K]+mid_dists[ind][rem].s[b-M*K]);
    }
    return res;
}

int main(){
    cin >> K >> N >> M >> O;
    for(int i = 1; i <= M; i++){
        int a, b, t;
        cin >> a >> b >> t;
        adj[a].pb(mp(b, t));
        radj[b].pb(mp(a, t));
    }
    genDists(0/K, (N-1)/K);

    for(int i = 1; i <= O; i++){
        int a, b;
        cin >> a >> b;
        int ans = getDist(a, b, 0/K, (N-1)/K);
        if(ans >= MOD) ans = -1;
        cout << ans << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 161 ms 42984 KB Output is correct
2 Correct 26 ms 33388 KB Output is correct
3 Correct 27 ms 33388 KB Output is correct
4 Correct 23 ms 33408 KB Output is correct
5 Correct 27 ms 33644 KB Output is correct
6 Correct 27 ms 33772 KB Output is correct
7 Correct 27 ms 33644 KB Output is correct
8 Correct 144 ms 42860 KB Output is correct
9 Correct 141 ms 42860 KB Output is correct
10 Correct 69 ms 39040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 274 ms 45804 KB Output is correct
2 Correct 23 ms 33408 KB Output is correct
3 Correct 23 ms 33388 KB Output is correct
4 Correct 23 ms 33388 KB Output is correct
5 Correct 23 ms 33388 KB Output is correct
6 Correct 23 ms 33516 KB Output is correct
7 Correct 52 ms 33772 KB Output is correct
8 Correct 54 ms 33792 KB Output is correct
9 Correct 143 ms 42860 KB Output is correct
10 Correct 475 ms 50156 KB Output is correct
11 Correct 279 ms 46060 KB Output is correct
12 Correct 196 ms 47664 KB Output is correct
13 Correct 526 ms 49000 KB Output is correct
14 Correct 262 ms 43756 KB Output is correct
15 Correct 295 ms 45292 KB Output is correct
16 Correct 287 ms 45344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 33504 KB Output is correct
2 Correct 23 ms 33388 KB Output is correct
3 Correct 23 ms 33388 KB Output is correct
4 Correct 23 ms 33388 KB Output is correct
5 Correct 23 ms 33388 KB Output is correct
6 Correct 25 ms 33644 KB Output is correct
7 Correct 27 ms 33644 KB Output is correct
8 Correct 35 ms 33772 KB Output is correct
9 Correct 30 ms 33772 KB Output is correct
10 Correct 118 ms 42732 KB Output is correct
11 Correct 258 ms 45804 KB Output is correct
12 Correct 417 ms 49900 KB Output is correct
13 Correct 460 ms 50796 KB Output is correct
14 Correct 360 ms 49004 KB Output is correct
15 Correct 285 ms 45164 KB Output is correct
16 Correct 276 ms 45164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 33504 KB Output is correct
2 Correct 23 ms 33388 KB Output is correct
3 Correct 23 ms 33388 KB Output is correct
4 Correct 23 ms 33388 KB Output is correct
5 Correct 23 ms 33388 KB Output is correct
6 Correct 25 ms 33644 KB Output is correct
7 Correct 27 ms 33644 KB Output is correct
8 Correct 35 ms 33772 KB Output is correct
9 Correct 30 ms 33772 KB Output is correct
10 Correct 118 ms 42732 KB Output is correct
11 Correct 258 ms 45804 KB Output is correct
12 Correct 417 ms 49900 KB Output is correct
13 Correct 460 ms 50796 KB Output is correct
14 Correct 360 ms 49004 KB Output is correct
15 Correct 285 ms 45164 KB Output is correct
16 Correct 276 ms 45164 KB Output is correct
17 Correct 258 ms 45676 KB Output is correct
18 Correct 23 ms 33388 KB Output is correct
19 Correct 23 ms 33388 KB Output is correct
20 Correct 23 ms 33388 KB Output is correct
21 Correct 24 ms 33388 KB Output is correct
22 Correct 23 ms 33388 KB Output is correct
23 Correct 34 ms 33772 KB Output is correct
24 Correct 34 ms 33664 KB Output is correct
25 Correct 39 ms 33900 KB Output is correct
26 Correct 37 ms 33772 KB Output is correct
27 Correct 125 ms 42732 KB Output is correct
28 Correct 436 ms 50156 KB Output is correct
29 Correct 479 ms 50796 KB Output is correct
30 Correct 355 ms 48972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 42984 KB Output is correct
2 Correct 26 ms 33388 KB Output is correct
3 Correct 27 ms 33388 KB Output is correct
4 Correct 23 ms 33408 KB Output is correct
5 Correct 27 ms 33644 KB Output is correct
6 Correct 27 ms 33772 KB Output is correct
7 Correct 27 ms 33644 KB Output is correct
8 Correct 144 ms 42860 KB Output is correct
9 Correct 141 ms 42860 KB Output is correct
10 Correct 69 ms 39040 KB Output is correct
11 Correct 274 ms 45804 KB Output is correct
12 Correct 23 ms 33408 KB Output is correct
13 Correct 23 ms 33388 KB Output is correct
14 Correct 23 ms 33388 KB Output is correct
15 Correct 23 ms 33388 KB Output is correct
16 Correct 23 ms 33516 KB Output is correct
17 Correct 52 ms 33772 KB Output is correct
18 Correct 54 ms 33792 KB Output is correct
19 Correct 143 ms 42860 KB Output is correct
20 Correct 475 ms 50156 KB Output is correct
21 Correct 279 ms 46060 KB Output is correct
22 Correct 196 ms 47664 KB Output is correct
23 Correct 526 ms 49000 KB Output is correct
24 Correct 262 ms 43756 KB Output is correct
25 Correct 295 ms 45292 KB Output is correct
26 Correct 287 ms 45344 KB Output is correct
27 Correct 23 ms 33504 KB Output is correct
28 Correct 23 ms 33388 KB Output is correct
29 Correct 23 ms 33388 KB Output is correct
30 Correct 23 ms 33388 KB Output is correct
31 Correct 23 ms 33388 KB Output is correct
32 Correct 25 ms 33644 KB Output is correct
33 Correct 27 ms 33644 KB Output is correct
34 Correct 35 ms 33772 KB Output is correct
35 Correct 30 ms 33772 KB Output is correct
36 Correct 118 ms 42732 KB Output is correct
37 Correct 258 ms 45804 KB Output is correct
38 Correct 417 ms 49900 KB Output is correct
39 Correct 460 ms 50796 KB Output is correct
40 Correct 360 ms 49004 KB Output is correct
41 Correct 285 ms 45164 KB Output is correct
42 Correct 276 ms 45164 KB Output is correct
43 Correct 258 ms 45676 KB Output is correct
44 Correct 23 ms 33388 KB Output is correct
45 Correct 23 ms 33388 KB Output is correct
46 Correct 23 ms 33388 KB Output is correct
47 Correct 24 ms 33388 KB Output is correct
48 Correct 23 ms 33388 KB Output is correct
49 Correct 34 ms 33772 KB Output is correct
50 Correct 34 ms 33664 KB Output is correct
51 Correct 39 ms 33900 KB Output is correct
52 Correct 37 ms 33772 KB Output is correct
53 Correct 125 ms 42732 KB Output is correct
54 Correct 436 ms 50156 KB Output is correct
55 Correct 479 ms 50796 KB Output is correct
56 Correct 355 ms 48972 KB Output is correct
57 Correct 687 ms 54240 KB Output is correct
58 Correct 150 ms 42860 KB Output is correct
59 Correct 309 ms 46188 KB Output is correct