답안 #930531

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930531 2024-02-20T05:49:42 Z dead0ne Toll (BOI17_toll) C++17
100 / 100
61 ms 37200 KB
#pragma GCC optimize("unroll-loops,Ofast,O3")
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define spc << " " <<
#define endl "\n"
#define all(x) x.begin(), x.end()
#define int long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define inf 1000000009
#define MOD 1000000007
#define MX 50005
using namespace std;


int k,n,m,o;
int edge[MX][5], dat[16][MX][5], mask[MX];

void divi(int l, int r, int lev){
    if(l>=r) return;
    int m = (l+r)/2;

    for(int i=0; i<k; i++){
        dat[lev][m*k+i][i] = 0;
        //dat[lev][m*k+i] = 0;
    }
    for(int i=m-1; i>=l; i--){
        for(int j=0; j<k; j++){
            for(int ww=0; ww<k; ww++){
                for(int uwu=0; uwu<k; uwu++){
                    dat[lev][i*k+j][uwu] = min(dat[lev][i*k+j][uwu], dat[lev][(i+1)*k+ww][uwu] + edge[i*k+j][ww]);
                }
            }
        }
    }

    for(int i=m+1; i<=r; i++){
        for(int j=0; j<k; j++){
            for(int ww=0; ww<k; ww++){
                for(int uwu=0; uwu<k; uwu++){
                    dat[lev][i*k+j][uwu] = min(dat[lev][i*k+j][uwu], dat[lev][(i-1)*k+ww][uwu] + edge[(i-1)*k+ww][j]);
                }
            }
        }
    }
    for(int i=m+1; i<=r; i++){
        mask[i] ^= (1<<lev);
    }

    divi(l, m, lev+1);
    divi(m+1, r, lev+1);
}



void solve(){
    cin >> k >> n >> m >> o;
    n += k-(n%k)-1;
    for(int i=0; i<n; i++) for(int j=0; j<k; j++) edge[i][j] = inf;
    for(int i=0; i<16; i++) for(int j=0; j<n; j++) for(int ww=0; ww<5; ww++) dat[i][j][ww] = inf;
    for(int i=0; i<m; i++){
        int a,b,c; cin >> a >> b >> c;
        edge[a][b%k] = c;
    }

    divi(0, n/k, 0);
    /*for(int i=0; i<n; i++){
        cerr << "---" spc i spc "---\n";
        for(int j=0; j<k; j++) cerr << j spc dat[0][i][j] << endl;
    }

    for(int i=0; i<n; i++) cerr << i spc mask[i] << endl;

    cerr << ":::  TO QUERIES  :::\n";*/
    while(o--){
        int a,b; cin >> a >> b;
        if(a == b){
            cout << 0 << endl;
            continue;
        }
        if(b/k <= a/k){
            cout << -1 << endl;
            continue;
        }

        int bits = __builtin_ctz(mask[a/k] ^ mask[b/k]);
        //cerr << bits << endl;
        int ans = inf;
        for(int i=0; i<k; i++) ans = min(ans, dat[bits][a][i] + dat[bits][b][i]);
        if(ans >= inf){
            cout << -1 << endl;
        }
        else{
            cout << ans << endl;
        }
    }
}



signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    #ifdef Local
    freopen("in","r",stdin);
    freopen("out","w",stdout);
    #endif

    /*freopen("fcolor.in","r",stdin);
    freopen("fcolor.out","w",stdout);*/

    int t=1;
    //cin >> t;
    while(t--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 34908 KB Output is correct
2 Correct 4 ms 33116 KB Output is correct
3 Correct 4 ms 33116 KB Output is correct
4 Correct 4 ms 33116 KB Output is correct
5 Correct 5 ms 33300 KB Output is correct
6 Correct 5 ms 33240 KB Output is correct
7 Correct 5 ms 33116 KB Output is correct
8 Correct 27 ms 34936 KB Output is correct
9 Correct 25 ms 34896 KB Output is correct
10 Correct 16 ms 34140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 35672 KB Output is correct
2 Correct 4 ms 33112 KB Output is correct
3 Correct 4 ms 33112 KB Output is correct
4 Correct 5 ms 33112 KB Output is correct
5 Correct 5 ms 33116 KB Output is correct
6 Correct 5 ms 33116 KB Output is correct
7 Correct 6 ms 33256 KB Output is correct
8 Correct 8 ms 33372 KB Output is correct
9 Correct 33 ms 34900 KB Output is correct
10 Correct 43 ms 36220 KB Output is correct
11 Correct 33 ms 35672 KB Output is correct
12 Correct 32 ms 35152 KB Output is correct
13 Correct 46 ms 35668 KB Output is correct
14 Correct 28 ms 34648 KB Output is correct
15 Correct 33 ms 34396 KB Output is correct
16 Correct 37 ms 34540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 33116 KB Output is correct
2 Correct 4 ms 33224 KB Output is correct
3 Correct 4 ms 33256 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 4 ms 33116 KB Output is correct
6 Correct 5 ms 33116 KB Output is correct
7 Correct 5 ms 33116 KB Output is correct
8 Correct 5 ms 33116 KB Output is correct
9 Correct 5 ms 33116 KB Output is correct
10 Correct 21 ms 34696 KB Output is correct
11 Correct 36 ms 35408 KB Output is correct
12 Correct 40 ms 36228 KB Output is correct
13 Correct 42 ms 36460 KB Output is correct
14 Correct 35 ms 35668 KB Output is correct
15 Correct 35 ms 34648 KB Output is correct
16 Correct 32 ms 34468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 33116 KB Output is correct
2 Correct 4 ms 33224 KB Output is correct
3 Correct 4 ms 33256 KB Output is correct
4 Correct 5 ms 33116 KB Output is correct
5 Correct 4 ms 33116 KB Output is correct
6 Correct 5 ms 33116 KB Output is correct
7 Correct 5 ms 33116 KB Output is correct
8 Correct 5 ms 33116 KB Output is correct
9 Correct 5 ms 33116 KB Output is correct
10 Correct 21 ms 34696 KB Output is correct
11 Correct 36 ms 35408 KB Output is correct
12 Correct 40 ms 36228 KB Output is correct
13 Correct 42 ms 36460 KB Output is correct
14 Correct 35 ms 35668 KB Output is correct
15 Correct 35 ms 34648 KB Output is correct
16 Correct 32 ms 34468 KB Output is correct
17 Correct 32 ms 35532 KB Output is correct
18 Correct 5 ms 33228 KB Output is correct
19 Correct 5 ms 33116 KB Output is correct
20 Correct 4 ms 33280 KB Output is correct
21 Correct 4 ms 33116 KB Output is correct
22 Correct 5 ms 33232 KB Output is correct
23 Correct 6 ms 33116 KB Output is correct
24 Correct 6 ms 33248 KB Output is correct
25 Correct 6 ms 33372 KB Output is correct
26 Correct 6 ms 33372 KB Output is correct
27 Correct 21 ms 34904 KB Output is correct
28 Correct 43 ms 36256 KB Output is correct
29 Correct 53 ms 36484 KB Output is correct
30 Correct 37 ms 35832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 34908 KB Output is correct
2 Correct 4 ms 33116 KB Output is correct
3 Correct 4 ms 33116 KB Output is correct
4 Correct 4 ms 33116 KB Output is correct
5 Correct 5 ms 33300 KB Output is correct
6 Correct 5 ms 33240 KB Output is correct
7 Correct 5 ms 33116 KB Output is correct
8 Correct 27 ms 34936 KB Output is correct
9 Correct 25 ms 34896 KB Output is correct
10 Correct 16 ms 34140 KB Output is correct
11 Correct 35 ms 35672 KB Output is correct
12 Correct 4 ms 33112 KB Output is correct
13 Correct 4 ms 33112 KB Output is correct
14 Correct 5 ms 33112 KB Output is correct
15 Correct 5 ms 33116 KB Output is correct
16 Correct 5 ms 33116 KB Output is correct
17 Correct 6 ms 33256 KB Output is correct
18 Correct 8 ms 33372 KB Output is correct
19 Correct 33 ms 34900 KB Output is correct
20 Correct 43 ms 36220 KB Output is correct
21 Correct 33 ms 35672 KB Output is correct
22 Correct 32 ms 35152 KB Output is correct
23 Correct 46 ms 35668 KB Output is correct
24 Correct 28 ms 34648 KB Output is correct
25 Correct 33 ms 34396 KB Output is correct
26 Correct 37 ms 34540 KB Output is correct
27 Correct 4 ms 33116 KB Output is correct
28 Correct 4 ms 33224 KB Output is correct
29 Correct 4 ms 33256 KB Output is correct
30 Correct 5 ms 33116 KB Output is correct
31 Correct 4 ms 33116 KB Output is correct
32 Correct 5 ms 33116 KB Output is correct
33 Correct 5 ms 33116 KB Output is correct
34 Correct 5 ms 33116 KB Output is correct
35 Correct 5 ms 33116 KB Output is correct
36 Correct 21 ms 34696 KB Output is correct
37 Correct 36 ms 35408 KB Output is correct
38 Correct 40 ms 36228 KB Output is correct
39 Correct 42 ms 36460 KB Output is correct
40 Correct 35 ms 35668 KB Output is correct
41 Correct 35 ms 34648 KB Output is correct
42 Correct 32 ms 34468 KB Output is correct
43 Correct 32 ms 35532 KB Output is correct
44 Correct 5 ms 33228 KB Output is correct
45 Correct 5 ms 33116 KB Output is correct
46 Correct 4 ms 33280 KB Output is correct
47 Correct 4 ms 33116 KB Output is correct
48 Correct 5 ms 33232 KB Output is correct
49 Correct 6 ms 33116 KB Output is correct
50 Correct 6 ms 33248 KB Output is correct
51 Correct 6 ms 33372 KB Output is correct
52 Correct 6 ms 33372 KB Output is correct
53 Correct 21 ms 34904 KB Output is correct
54 Correct 43 ms 36256 KB Output is correct
55 Correct 53 ms 36484 KB Output is correct
56 Correct 37 ms 35832 KB Output is correct
57 Correct 61 ms 37200 KB Output is correct
58 Correct 25 ms 34908 KB Output is correct
59 Correct 35 ms 35676 KB Output is correct