이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |