답안 #557593

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557593 2022-05-05T14:31:57 Z fatemetmhr Toll (BOI17_toll) C++17
100 / 100
616 ms 16448 KB
// `Be name khoda` //

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb     push_back
#define all(x) x.begin(), x.end()
#define fi     first
#define se     second


const int maxn5 = 1e5 + 10;
const int inf   = 1e9;

int ssq   = 100;
int h[maxn5], h2[maxn5], dis[2][maxn5][6];
int av[maxn5], av2[maxn5];
vector <pair<int, int>> jda[maxn5], adj[maxn5];
int k;

inline int get_dis(int a, int b){
    fill(h + a, h + b + 5, inf);
    h[b] = 0;
    for(int i = b; i > a; i--){
        for(auto [u, t] : jda[i])
            h[u] = min(h[u], h[i] + t);
    }
    return h[a];
}

inline void build(int id){
    for(int i = id; i < id + ssq; i++){
        fill(h + id, h + id + ssq + 3, inf);
        h[i] = 0;
        for(int j = i; j < id + ssq; j++) for(auto [u, t] : adj[j])
            h[u] = min(h[u], h[j] + t);
        for(int j = i; j >= id; j--) for(auto [u, t] : jda[j])
            h[u] = min(h[u], h[j] + t);
        for(int j = 0; j < k; j++){
            dis[0][i][j] = h[id + j];
            dis[1][i][j] = h[id + ssq - k + j];
        }
    }
    return;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int n, m, q; cin >> k >> n >> m >> q;
    ssq *= k;
    for(int i = 0; i < m; i++){
        int a, b, t; cin >> a >> b >> t;
        jda[b].pb({a, t});
        adj[a].pb({b, t});
    }
    for(int i = 0; i < n; i+=ssq)
        build(i);
    for(int i = 0; i < q; i++){
        int a, b; cin >> a >> b;
        int cmpa = a / ssq * ssq;
        int cmpb = b / ssq * ssq;
        if(cmpa == cmpb){
            int ans = get_dis(a, b);
            if(ans == inf)
                ans = -1;
            cout << ans << '\n';
            continue;
        }
        for(int i = 0; i < k; i++){ // akhare cmpa
            av[i] = cmpa + ssq - k + i;
            h[i] = dis[1][a][i];
            h2[i] = inf;
        }
        for(int i = 0; i < k; i++) for(auto [u, t] : adj[av[i]])
            h2[u - cmpa - ssq] = min(h2[u - cmpa - ssq], h[i] + t);
        for(int i = 0; i < k; i++){ // avale cmpa + ssq
            av[i] = cmpa + ssq + i;
            h[i] = h2[i];
            h2[i] = inf;
        }
        cmpa += ssq;
        while(cmpa != cmpb){
            //cout << "hey " << cmpa << ' ' << cmpb << endl;
            //for(int i = 0; i < k; i++)
                //cout << av[i] << ' ' << h[i] << endl;
            for(int i = 0; i < k; i++){ // avale cmpa
                av2[i] = cmpa + ssq - k + i;
                h2[i] = inf;
            }
            for(int i = 0; i < k; i++) for(int j = 0; j < k; j++)
                h2[j] = min(h2[j], dis[1][av[i]][j] + h[i]);
            for(int i = 0; i < k; i++){ // akhare cmpa
                av[i] = av2[i];
                h[i] = h2[i];
                h2[i] = inf;
            }
            for(int i = 0; i < k; i++) for(auto [u, t] : adj[av[i]])
                h2[u - cmpa - ssq] = min(h2[u - cmpa - ssq], h[i] + t);
            for(int i = 0; i < k; i++){ // avale cmpa + ssq
                av[i] = cmpa + ssq + i;
                h[i] = h2[i];
            }
            cmpa += ssq;
        }
        int ans = inf;
        for(int i = 0; i < k; i++)
            ans = min(ans, h[i] + dis[0][b][i]);
        if(ans == inf)
            ans = -1;
        cout << ans << '\n';
    }
}

/*
1 11 10 1
0 1 4
1 2 20
2 3 21
3 4 24
4 5 26
5 6 21
6 7 8
7 8 23
8 9 24
9 10 15
0 10
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 10820 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5104 KB Output is correct
8 Correct 121 ms 10728 KB Output is correct
9 Correct 127 ms 10460 KB Output is correct
10 Correct 55 ms 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 245 ms 10676 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5052 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 4948 KB Output is correct
7 Correct 7 ms 5292 KB Output is correct
8 Correct 10 ms 5204 KB Output is correct
9 Correct 133 ms 11688 KB Output is correct
10 Correct 353 ms 13632 KB Output is correct
11 Correct 270 ms 11724 KB Output is correct
12 Correct 241 ms 11468 KB Output is correct
13 Correct 468 ms 12980 KB Output is correct
14 Correct 200 ms 10956 KB Output is correct
15 Correct 282 ms 10108 KB Output is correct
16 Correct 298 ms 10128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 14 ms 5204 KB Output is correct
9 Correct 10 ms 5180 KB Output is correct
10 Correct 64 ms 10700 KB Output is correct
11 Correct 148 ms 10688 KB Output is correct
12 Correct 319 ms 12456 KB Output is correct
13 Correct 302 ms 13012 KB Output is correct
14 Correct 257 ms 11756 KB Output is correct
15 Correct 271 ms 9308 KB Output is correct
16 Correct 291 ms 9164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 6 ms 5204 KB Output is correct
8 Correct 14 ms 5204 KB Output is correct
9 Correct 10 ms 5180 KB Output is correct
10 Correct 64 ms 10700 KB Output is correct
11 Correct 148 ms 10688 KB Output is correct
12 Correct 319 ms 12456 KB Output is correct
13 Correct 302 ms 13012 KB Output is correct
14 Correct 257 ms 11756 KB Output is correct
15 Correct 271 ms 9308 KB Output is correct
16 Correct 291 ms 9164 KB Output is correct
17 Correct 168 ms 10716 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 5068 KB Output is correct
23 Correct 4 ms 5172 KB Output is correct
24 Correct 6 ms 5076 KB Output is correct
25 Correct 18 ms 5204 KB Output is correct
26 Correct 12 ms 5176 KB Output is correct
27 Correct 92 ms 10648 KB Output is correct
28 Correct 326 ms 12456 KB Output is correct
29 Correct 307 ms 13004 KB Output is correct
30 Correct 285 ms 11776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 10820 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5104 KB Output is correct
8 Correct 121 ms 10728 KB Output is correct
9 Correct 127 ms 10460 KB Output is correct
10 Correct 55 ms 7544 KB Output is correct
11 Correct 245 ms 10676 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5052 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 7 ms 5292 KB Output is correct
18 Correct 10 ms 5204 KB Output is correct
19 Correct 133 ms 11688 KB Output is correct
20 Correct 353 ms 13632 KB Output is correct
21 Correct 270 ms 11724 KB Output is correct
22 Correct 241 ms 11468 KB Output is correct
23 Correct 468 ms 12980 KB Output is correct
24 Correct 200 ms 10956 KB Output is correct
25 Correct 282 ms 10108 KB Output is correct
26 Correct 298 ms 10128 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 4 ms 5076 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 14 ms 5204 KB Output is correct
35 Correct 10 ms 5180 KB Output is correct
36 Correct 64 ms 10700 KB Output is correct
37 Correct 148 ms 10688 KB Output is correct
38 Correct 319 ms 12456 KB Output is correct
39 Correct 302 ms 13012 KB Output is correct
40 Correct 257 ms 11756 KB Output is correct
41 Correct 271 ms 9308 KB Output is correct
42 Correct 291 ms 9164 KB Output is correct
43 Correct 168 ms 10716 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 3 ms 4948 KB Output is correct
48 Correct 3 ms 5068 KB Output is correct
49 Correct 4 ms 5172 KB Output is correct
50 Correct 6 ms 5076 KB Output is correct
51 Correct 18 ms 5204 KB Output is correct
52 Correct 12 ms 5176 KB Output is correct
53 Correct 92 ms 10648 KB Output is correct
54 Correct 326 ms 12456 KB Output is correct
55 Correct 307 ms 13004 KB Output is correct
56 Correct 285 ms 11776 KB Output is correct
57 Correct 616 ms 16448 KB Output is correct
58 Correct 129 ms 11552 KB Output is correct
59 Correct 212 ms 12444 KB Output is correct