제출 #392209

#제출 시각아이디문제언어결과실행 시간메모리
392209JerryLiu06Toll (BOI17_toll)C++11
100 / 100
323 ms101476 KiB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

#define pb push_back

#define f first
#define s second

int K, N, M, O; int DP[50010][20][5][5], CUR[5][5];

void comb(int res[5][5], int A[5][5], int B[5][5]) {
    for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) {
        for (int k = 0; k < 5; k++) res[i][j] = min(res[i][j], A[i][k] + B[k][j]);
    }
}

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

    cin >> K >> N >> M >> O; memset(DP, 0x3f, sizeof DP);

    for (int i = 0; i < M; i++) { 
        int A, B, T; cin >> A >> B >> T; DP[A / K][0][A % K][B % K] = T;
    }
    for (int j = 1; j < 20; j++) for (int i = 0; i + (1 << (j - 1)) <= N / K; i++) {
        comb(DP[i][j], DP[i][j - 1], DP[i + (1 << (j - 1))][j - 1]);
    }
    for (int i = 0; i < O; i++) {
        int A, B; cin >> A >> B; memset(CUR, 0x3f, sizeof CUR);

        for (int j = 0; j < K; j++) CUR[j][j] = 0; 
        
        int LEN = B / K - A / K; int ST = A / K;

        for (int i = 20; i >= 0; i--) if (LEN & (1 << i)) {
            int TMP[5][5]; memset(TMP, 0x3f, sizeof TMP);

            comb(TMP, CUR, DP[ST][i]); memcpy(CUR, TMP, sizeof CUR); ST += (1 << i);
        }
        cout << (CUR[A % K][B % K] == 0x3f3f3f3f ? -1 : CUR[A % K][B % K]) << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...