답안 #676444

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676444 2022-12-30T23:01:00 Z bogdanvladmihai Toll (BOI17_toll) C++14
0 / 100
244 ms 83748 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int INF = (int)1e9;

int K, N, M, Q;

vector<vector<vector<vector<int>>>> dp;

signed main() {
    cin >> K >> N >> M >> Q;

    dp.resize((N / K + 1) + 2, vector<vector<vector<int>>>(ceil(log2(N / K)) + 3, 
                            vector<vector<int>>(K, vector<int>(K, INF))));
    for (int i = 0; i < M; i++) {
        int a, b, c; cin >> a >> b >> c;
        dp[a / K][0][a % K][b % K] = c;
    }

    for (int l = 1; (1 << (l - 1)) <= (N + K - 1) / K; l++) {
        for (int g = 0; g + (1 << l) < (N + K - 1) / K; g++) {
            for (int x = 0; x < K; x++) {
                for (int y = 0; y < K; y++) {
                    for (int z = 0; z < K; z++) {
                        dp[g][l][x][y] = min(dp[g][l][x][y], 
                                            dp[g][l - 1][x][z] + dp[g + (1 << (l - 1))][l - 1][z][y]);
                    }
                }
            }
        }
    }

    for (int i = 0; i < Q; i++) {
        int a, b; cin >> a >> b;

        vector<vector<int>> answer(K, vector<int>(K, INF));
        for (int j = 0; j < K; j++) {
            answer[j][j] = 0;
        }

        int len = b / K - a / K, lst = a / K;
        for (int l = ceil(log2(len)) + 2; l >= 0; l--) {
            if (lst + (1 << l) < b / K) {
                vector<vector<int>> tmp(K, vector<int>(K, INF));
                for (int x = 0; x < K; x++) {
                    for (int y = 0; y < K; y++) {
                        for (int z = 0; z < K; z++) {
                            tmp[x][y] = min(tmp[x][y], answer[x][z] + dp[lst][l][z][y]);
                        }
                    }
                }

                answer = tmp;
                lst += (1 << l);
            }
        }

        if (answer[a % K][b % K] == INF) {
            cout << "-1\n";
        } else {
            cout << answer[a % K][b % K] << "\n";
        }
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 200 ms 83748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 244 ms 68264 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 200 ms 83748 KB Output isn't correct
2 Halted 0 ms 0 KB -