제출 #462452

#제출 시각아이디문제언어결과실행 시간메모리
462452JovanBToll (BOI17_toll)C++17
100 / 100
164 ms196960 KiB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
#define pb push_back
 
const int INF = 1000000000;
ll dp[50500][20][5][5];
const int MXLOG = 20;
 
int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;
 
    int k, n, q, m;
    cin >> k >> n >> m >> q;
    for(int i=0; i<=n/k; i++){
        for(int j=0; j<k; j++){
            for(int g=0; g<k; g++){
                dp[i][0][j][g] = 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 j=1; j<MXLOG; j++){
        for(int i=0; i<=n/k; i++){
            for(int md=0; md<k; md++){
                for(int fn=0; fn<k; fn++){
                    dp[i][j][md][fn] = INF;
                    if(i+(1<<j) > n/k) continue;
                    for(int sr=0; sr<k; sr++){
                        dp[i][j][md][fn] = min(dp[i][j][md][fn], dp[i][j-1][md][sr] + dp[i+(1<<(j-1))][j-1][sr][fn]);
                    }
                }
            }
        }
    }
    while(q--){
        int a, b;
        cin >> a >> b;
        int x = a/k;
        int y = b/k;
        vector <ll> res;
        for(int i=0; i<k; i++) res.push_back(INF);
        res[a%k] = 0;
        for(int st=MXLOG-1; st>=0; st--){
            if(y-x >= (1<<st)){
                vector <ll> nres;
                for(int gg=0; gg<k; gg++) nres.push_back(INF);
                for(int pc=0; pc<k; pc++){
                    for(int fn=0; fn<k; fn++){
                        nres[fn] = min(nres[fn], res[pc] + dp[x][st][pc][fn]);
                    }
                }
                res = nres;
                x += (1<<st);
            }
        }
        if(res[b%k] >= INF) cout << "-1\n";
        else cout << res[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...