Submission #862649

# Submission time Handle Problem Language Result Execution time Memory
862649 2023-10-18T17:24:16 Z Ahmed57 Toll (BOI17_toll) C++17
100 / 100
342 ms 51732 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long k;vector<pair<int,int>> adj[100001],rev[1000001];
long long an[100001];
map<pair<int,int>,long long> mp;
void dc(int l,int r,vector<pair<pair<int,int>,int>> qu){
    if(l>=r)return;
    int md = (l+r)/2;
    long long ll[k][(int)k*(md-l+1)],rr[k][(k*(r-md))];
    for(int j = md;j>=l;j--){
        for(int z = 0;z<k;z++){
            for(int a = 0;a<k;a++){
                if(j==md){
                    if(z==a){
                        ll[a][((md+1)*k-1)-(j*k+z)] = 0;
                    }
                    else ll[a][((md+1)*k-1)-(j*k+z)] = 1e9;
                    continue;
                }
                ll[a][((md+1)*k-1)-(j*k+z)] = 1e9;
                for(auto e:adj[j*k+z]){
                    ll[a][((md+1)*k-1)-(j*k+z)] = min(ll[a][((md+1)*k-1)-(j*k+z)],ll[a][((md+1)*k-1)-e.first]+e.second);
                }
            }
        }
    }
    for(int j = md+1;j<=r;j++){
        for(int z = 0;z<k;z++){
            for(int a = 0;a<k;a++){
                if(j==md+1){
                    if(z==a)rr[a][(j*k+z)-((md+1)*k)] = 0;
                    else rr[a][(j*k+z)-((md+1)*k)] = 1e9;
                    continue;
                }
                rr[a][(j*k+z)-((md+1)*k)] = 1e9;
                for(auto e:rev[j*k+z]){
                    rr[a][(j*k+z)-((md+1)*k)] = min(rr[a][(j*k+z)-((md+1)*k)],rr[a][e.first-((md+1)*k)]+e.second);
                }
            }
        }
    }
    vector<pair<pair<int,int>,int>> R,L;
    for(auto i:qu){
        if(i.first.first/k>md){
            R.push_back(i);
        }else if(i.first.second/k<md){
            L.push_back(i);
        }else{
            if((i.first.second/k)==md){
                an[i.second] = ll[i.first.second%k][((md+1)*k-1)-i.first.first];
            }else{
                long long ans = 1e18;
                for(int z = 0;z<k;z++){
                    for(int e = 0;e<k;e++){
                        ans = min(ans,ll[z][((md+1)*k-1)-i.first.first]+rr[e][i.first.second-((md+1)*k)]+(mp.count(make_pair((md*k)+z,(md+1)*k+e))==0?1000000000:mp[{(md*k)+z,(md+1)*k+e}]));
                    }
                }
                an[i.second] = ans;
            }
        }
    }
    dc(l,md-1,L);dc(md+1,r,R);
}
int main(){
    memset(an,-1,sizeof an);
    int n,m,o;
    cin>>k>>n>>m>>o;
    for(int i = 0;i<m;i++){
        long long a,b,c;
        cin>>a>>b>>c;
        mp[{a,b}] = c;
        adj[a].push_back({b,c});
        rev[b].push_back({a,c});
    }
    vector<pair<pair<int,int>,int>> qu;
    for(int i = 0;i<o;i++){
        long long a,b;cin>>a>>b;
        if(a==b){
            an[i] = 0;continue;
        }
        qu.push_back({{a,b},i});
    }
    dc(0,(n-1)/k,qu);
    for(int i = 0;i<o;i++){
        cout<<(an[i]>=1e9?-1:an[i])<<endl;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 99 ms 35392 KB Output is correct
2 Correct 7 ms 26972 KB Output is correct
3 Correct 7 ms 26972 KB Output is correct
4 Correct 8 ms 26968 KB Output is correct
5 Correct 9 ms 27228 KB Output is correct
6 Correct 8 ms 27228 KB Output is correct
7 Correct 9 ms 27224 KB Output is correct
8 Correct 90 ms 35204 KB Output is correct
9 Correct 99 ms 35052 KB Output is correct
10 Correct 31 ms 28352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 39776 KB Output is correct
2 Correct 7 ms 27052 KB Output is correct
3 Correct 7 ms 26972 KB Output is correct
4 Correct 7 ms 26972 KB Output is correct
5 Correct 7 ms 26852 KB Output is correct
6 Correct 7 ms 26972 KB Output is correct
7 Correct 24 ms 27704 KB Output is correct
8 Correct 26 ms 27648 KB Output is correct
9 Correct 90 ms 35456 KB Output is correct
10 Correct 228 ms 45856 KB Output is correct
11 Correct 166 ms 40188 KB Output is correct
12 Correct 157 ms 38756 KB Output is correct
13 Correct 234 ms 47712 KB Output is correct
14 Correct 155 ms 38876 KB Output is correct
15 Correct 102 ms 38088 KB Output is correct
16 Correct 103 ms 37972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 27048 KB Output is correct
2 Correct 7 ms 27224 KB Output is correct
3 Correct 7 ms 27228 KB Output is correct
4 Correct 8 ms 26972 KB Output is correct
5 Correct 6 ms 26972 KB Output is correct
6 Correct 7 ms 27064 KB Output is correct
7 Correct 8 ms 27124 KB Output is correct
8 Correct 11 ms 27484 KB Output is correct
9 Correct 11 ms 27484 KB Output is correct
10 Correct 76 ms 34640 KB Output is correct
11 Correct 117 ms 38996 KB Output is correct
12 Correct 216 ms 45024 KB Output is correct
13 Correct 263 ms 46704 KB Output is correct
14 Correct 149 ms 42320 KB Output is correct
15 Correct 94 ms 37712 KB Output is correct
16 Correct 93 ms 37736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 27048 KB Output is correct
2 Correct 7 ms 27224 KB Output is correct
3 Correct 7 ms 27228 KB Output is correct
4 Correct 8 ms 26972 KB Output is correct
5 Correct 6 ms 26972 KB Output is correct
6 Correct 7 ms 27064 KB Output is correct
7 Correct 8 ms 27124 KB Output is correct
8 Correct 11 ms 27484 KB Output is correct
9 Correct 11 ms 27484 KB Output is correct
10 Correct 76 ms 34640 KB Output is correct
11 Correct 117 ms 38996 KB Output is correct
12 Correct 216 ms 45024 KB Output is correct
13 Correct 263 ms 46704 KB Output is correct
14 Correct 149 ms 42320 KB Output is correct
15 Correct 94 ms 37712 KB Output is correct
16 Correct 93 ms 37736 KB Output is correct
17 Correct 125 ms 39264 KB Output is correct
18 Correct 6 ms 26972 KB Output is correct
19 Correct 6 ms 26968 KB Output is correct
20 Correct 6 ms 26972 KB Output is correct
21 Correct 6 ms 26972 KB Output is correct
22 Correct 6 ms 27048 KB Output is correct
23 Correct 15 ms 27284 KB Output is correct
24 Correct 15 ms 27316 KB Output is correct
25 Correct 19 ms 27484 KB Output is correct
26 Correct 15 ms 27736 KB Output is correct
27 Correct 66 ms 34900 KB Output is correct
28 Correct 200 ms 45588 KB Output is correct
29 Correct 238 ms 46932 KB Output is correct
30 Correct 157 ms 42580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 35392 KB Output is correct
2 Correct 7 ms 26972 KB Output is correct
3 Correct 7 ms 26972 KB Output is correct
4 Correct 8 ms 26968 KB Output is correct
5 Correct 9 ms 27228 KB Output is correct
6 Correct 8 ms 27228 KB Output is correct
7 Correct 9 ms 27224 KB Output is correct
8 Correct 90 ms 35204 KB Output is correct
9 Correct 99 ms 35052 KB Output is correct
10 Correct 31 ms 28352 KB Output is correct
11 Correct 183 ms 39776 KB Output is correct
12 Correct 7 ms 27052 KB Output is correct
13 Correct 7 ms 26972 KB Output is correct
14 Correct 7 ms 26972 KB Output is correct
15 Correct 7 ms 26852 KB Output is correct
16 Correct 7 ms 26972 KB Output is correct
17 Correct 24 ms 27704 KB Output is correct
18 Correct 26 ms 27648 KB Output is correct
19 Correct 90 ms 35456 KB Output is correct
20 Correct 228 ms 45856 KB Output is correct
21 Correct 166 ms 40188 KB Output is correct
22 Correct 157 ms 38756 KB Output is correct
23 Correct 234 ms 47712 KB Output is correct
24 Correct 155 ms 38876 KB Output is correct
25 Correct 102 ms 38088 KB Output is correct
26 Correct 103 ms 37972 KB Output is correct
27 Correct 7 ms 27048 KB Output is correct
28 Correct 7 ms 27224 KB Output is correct
29 Correct 7 ms 27228 KB Output is correct
30 Correct 8 ms 26972 KB Output is correct
31 Correct 6 ms 26972 KB Output is correct
32 Correct 7 ms 27064 KB Output is correct
33 Correct 8 ms 27124 KB Output is correct
34 Correct 11 ms 27484 KB Output is correct
35 Correct 11 ms 27484 KB Output is correct
36 Correct 76 ms 34640 KB Output is correct
37 Correct 117 ms 38996 KB Output is correct
38 Correct 216 ms 45024 KB Output is correct
39 Correct 263 ms 46704 KB Output is correct
40 Correct 149 ms 42320 KB Output is correct
41 Correct 94 ms 37712 KB Output is correct
42 Correct 93 ms 37736 KB Output is correct
43 Correct 125 ms 39264 KB Output is correct
44 Correct 6 ms 26972 KB Output is correct
45 Correct 6 ms 26968 KB Output is correct
46 Correct 6 ms 26972 KB Output is correct
47 Correct 6 ms 26972 KB Output is correct
48 Correct 6 ms 27048 KB Output is correct
49 Correct 15 ms 27284 KB Output is correct
50 Correct 15 ms 27316 KB Output is correct
51 Correct 19 ms 27484 KB Output is correct
52 Correct 15 ms 27736 KB Output is correct
53 Correct 66 ms 34900 KB Output is correct
54 Correct 200 ms 45588 KB Output is correct
55 Correct 238 ms 46932 KB Output is correct
56 Correct 157 ms 42580 KB Output is correct
57 Correct 342 ms 51732 KB Output is correct
58 Correct 80 ms 35348 KB Output is correct
59 Correct 140 ms 40184 KB Output is correct