답안 #399548

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
399548 2021-05-06T03:32:22 Z Andyvanh1 Toll (BOI17_toll) C++14
100 / 100
305 ms 52288 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;



#define vt vector
#define pb push_back

typedef long long ll;
typedef vt<int> vi;


#define INF 2000000000

int N,M,K;

vt<vt<vt<vi>>> dp;

vt<vi> arr;
vt<vt<pair<int,int>>> adjlist;

void gen(){
    for(int f = 1; f < 17; f++){
        for(int i = 0; i < N; i++){
            for(int j = 0; j < K; j++){
                for(int l = 0; l < K; l++ ){
                    if(i+(1<<(f-1))<N) {
                        for(int a = 0; a < K; a++){
                            dp[i][j][f][l] = min(dp[i][j][f][l],dp[i][j][f - 1][a] + dp[i + (1 << (f - 1))][a][f - 1][l]);
                        }

                    }
                }
            }
        }
    }

}

int get(int i, int j, int f, int l){
    vi cur(K);
    f--;
    if(f==-1)return -1;
    for(int a = 0; a < K; a++){
        cur[a] = dp[i][j][0][a];
    }
    i++;

    for(int a = 0; a < 17; a++){

        if((1<<a)&f){
            vi part(K);
            for(int b = 0; b < K; b++){
                part[b] = INF/3;
            }
            for(int b = 0; b < K; b++){
                for(int c = 0; c < K; c++){
                    if(dp[i][c][a][b]!=INF/3){
                        part[b] = min(part[b],cur[c]+dp[i][c][a][b]);
                    }
                }
            }
            i+=(1<<a);
            for(int b = 0; b < K; b++){
                cur[b] = part[b];
            }

        }

    }
    if(cur[l]==INF/3){
        return -1;
    }
    return cur[l];

}

void solve(){
    int  m, n, o;
    cin>>K>>n>>m>>o;
    N = (n-1)/K+1;

    dp.resize(N,vt<vt<vi>>(K,vt<vi>(17,vi(K))));
    
    for(int i = 0; i < N; i++){
        for(int j = 0; j < K; j++){
            for(int l = 0; l < K; l++){
                for(int f = 0; f < 17; f++) {
                    dp[i][j][f][l] = INF / 3;
                }
            }
        }
    }


    for(int i = 0; i < m; i++){
        int u,v,w;
        cin>>u>>v>>w;
        dp[u/K][u%K][0][v%K] = w;
    }

    gen();

    for(int i = 0; i < o; i++){
        int u,v;
        cin>>u>>v;
        cout<<get(u/K,u%K,(v/K-u/K),v%K)<<'\n';
    }

}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    //cin>>t;
    for(int i = 0; i < t; i++){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 50020 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 3 ms 1228 KB Output is correct
6 Correct 3 ms 1232 KB Output is correct
7 Correct 4 ms 1312 KB Output is correct
8 Correct 163 ms 50000 KB Output is correct
9 Correct 166 ms 50016 KB Output is correct
10 Correct 131 ms 50016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 49476 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 7 ms 1352 KB Output is correct
8 Correct 8 ms 1352 KB Output is correct
9 Correct 140 ms 50896 KB Output is correct
10 Correct 271 ms 51296 KB Output is correct
11 Correct 240 ms 51164 KB Output is correct
12 Correct 255 ms 50304 KB Output is correct
13 Correct 184 ms 31704 KB Output is correct
14 Correct 158 ms 30912 KB Output is correct
15 Correct 147 ms 30616 KB Output is correct
16 Correct 147 ms 30600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 3 ms 1228 KB Output is correct
7 Correct 3 ms 1228 KB Output is correct
8 Correct 4 ms 1344 KB Output is correct
9 Correct 4 ms 1228 KB Output is correct
10 Correct 137 ms 50704 KB Output is correct
11 Correct 231 ms 51016 KB Output is correct
12 Correct 256 ms 51120 KB Output is correct
13 Correct 265 ms 51288 KB Output is correct
14 Correct 254 ms 50784 KB Output is correct
15 Correct 144 ms 30564 KB Output is correct
16 Correct 137 ms 30532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 3 ms 1228 KB Output is correct
7 Correct 3 ms 1228 KB Output is correct
8 Correct 4 ms 1344 KB Output is correct
9 Correct 4 ms 1228 KB Output is correct
10 Correct 137 ms 50704 KB Output is correct
11 Correct 231 ms 51016 KB Output is correct
12 Correct 256 ms 51120 KB Output is correct
13 Correct 265 ms 51288 KB Output is correct
14 Correct 254 ms 50784 KB Output is correct
15 Correct 144 ms 30564 KB Output is correct
16 Correct 137 ms 30532 KB Output is correct
17 Correct 233 ms 51028 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 4 ms 1356 KB Output is correct
24 Correct 6 ms 1356 KB Output is correct
25 Correct 7 ms 1352 KB Output is correct
26 Correct 6 ms 1364 KB Output is correct
27 Correct 146 ms 50764 KB Output is correct
28 Correct 274 ms 51140 KB Output is correct
29 Correct 274 ms 51424 KB Output is correct
30 Correct 260 ms 50828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 50020 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 3 ms 1228 KB Output is correct
6 Correct 3 ms 1232 KB Output is correct
7 Correct 4 ms 1312 KB Output is correct
8 Correct 163 ms 50000 KB Output is correct
9 Correct 166 ms 50016 KB Output is correct
10 Correct 131 ms 50016 KB Output is correct
11 Correct 239 ms 49476 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 7 ms 1352 KB Output is correct
18 Correct 8 ms 1352 KB Output is correct
19 Correct 140 ms 50896 KB Output is correct
20 Correct 271 ms 51296 KB Output is correct
21 Correct 240 ms 51164 KB Output is correct
22 Correct 255 ms 50304 KB Output is correct
23 Correct 184 ms 31704 KB Output is correct
24 Correct 158 ms 30912 KB Output is correct
25 Correct 147 ms 30616 KB Output is correct
26 Correct 147 ms 30600 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 3 ms 1228 KB Output is correct
33 Correct 3 ms 1228 KB Output is correct
34 Correct 4 ms 1344 KB Output is correct
35 Correct 4 ms 1228 KB Output is correct
36 Correct 137 ms 50704 KB Output is correct
37 Correct 231 ms 51016 KB Output is correct
38 Correct 256 ms 51120 KB Output is correct
39 Correct 265 ms 51288 KB Output is correct
40 Correct 254 ms 50784 KB Output is correct
41 Correct 144 ms 30564 KB Output is correct
42 Correct 137 ms 30532 KB Output is correct
43 Correct 233 ms 51028 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 4 ms 1356 KB Output is correct
50 Correct 6 ms 1356 KB Output is correct
51 Correct 7 ms 1352 KB Output is correct
52 Correct 6 ms 1364 KB Output is correct
53 Correct 146 ms 50764 KB Output is correct
54 Correct 274 ms 51140 KB Output is correct
55 Correct 274 ms 51424 KB Output is correct
56 Correct 260 ms 50828 KB Output is correct
57 Correct 305 ms 52288 KB Output is correct
58 Correct 162 ms 50952 KB Output is correct
59 Correct 258 ms 51288 KB Output is correct