Submission #1210619

#TimeUsernameProblemLanguageResultExecution timeMemory
1210619vaneaToll (BOI17_toll)C++20
0 / 100
9 ms20036 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int mxN = 1e4+10;

int dp[mxN][20][5][5], ans[5][5], tmp[5][5];

void comb(int dp[5][5], int a[5][5], int b[5][5], int k) {
    for(int x = 0; x < k; x++) {
        for(int y = 0; y < k; y++) {
            for(int z = 0; z < k; z++) {
                dp[x][y] = min(dp[x][y], a[x][z] + b[z][y]);
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int k, n, m, q;
    cin >> k >> n >> m >> q;
    memset(dp, 0x3F, sizeof dp);
    for(int i = 0; i < m; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        dp[a/k][0][a%k][b%k] = w;
    }
    for(int j = 1; j < 17; j++) {
        for(int i = 0; i + (1 << j) < (n + k - 1) / k; i++) {
            comb(dp[i][j], dp[i][j-1], dp[i+(1 << (j-1))][j-1], k);
        }
    }
    while(q--) {
        int a, b;
        cin >> a >> b;
        if(a > b) {
            cout << -1 << '\n';
            continue;
        }
        int dist = (b/k) - (a/k), curr = a/k;
        memset(ans, 0x3F, sizeof ans);
        for(int i = 0; i < k; i++) ans[i][i] = 0;
        for(int j = 16; j >= 0; j--) {
            if(dist & (1 << j)) {
                memset(tmp, 0x3F, sizeof tmp);
                comb(tmp, ans, dp[curr][j], k);
                memcpy(ans, tmp, sizeof ans);
                curr += (1 << j);
            }
        }
        cout << (ans[a%k][b%k] == 0x3F3F3F3F ? -1 : ans[a%k][b%k]) << '\n';
    }
    return 0;
}
#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...