제출 #781363

#제출 시각아이디문제언어결과실행 시간메모리
781363CookieToll (BOI17_toll)C++14
100 / 100
80 ms79600 KiB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 5e4 + 1, mxq = 2e5 + 5, sq = 400, mxv = 2e7 + 5;
//const int base = (1 << 18);
const ll inf = 1e9 + 5, neg = -69420;

int k, n, m, o;
struct MAT{
    int a[5][5];
    void init(){
        for(int i = 0; i < k; i++){
            for(int j = 0; j < k; j++){
                a[i][j] = inf;
            }
        }
    }
    MAT operator *(const MAT &b){
        MAT res;
        for(int i = 0; i < k; i++){
            for(int j = 0; j < k; j++){
                res.a[i][j] = inf;
                for(int l = 0; l < k; l++){
                    res.a[i][j] = min(res.a[i][j], a[i][l] + b.a[l][j]);
                }
            }
        }
        return(res);
    }
};
MAT dp[mxn + 1][16];
void input(){
    cin >> k >> n >> m >> o;
    for(int i = 0; i <= (n - 1) / k; i++){
        for(int j = 0; j < 16; j++){
            dp[i][j].init();
        }
    }
    for(int i = 0; i < m; i++){
        int u, v, w; cin >> u >> v >> w;
        dp[u / k][0].a[u % k][v % k] = w;
    }

}
void process(){
    int mx = (n - 1) / k;
    for(int i = 1; i < 16; i++){
        for(int j = 0; j + (1 << i) - 1 <= mx; j++){
            dp[j][i] = dp[j][i - 1] * dp[j + (1 << (i - 1))][i - 1];
        }
    }
    
    for(int i = 0; i < o; i++){
        int u, v; cin >> u >> v;
        MAT ans; ans.init();
        forr(j, 0, k){
            ans.a[j][j] = 0;
        }
        int uu = (u / k), vv = (v / k);
        int dif = (vv - uu);
        for(int j = 0; j < 16; j++){
            if(dif & (1 << j)){
                ans = ans * dp[uu][j]; uu += (1 << j);
            }
        }
        cout << ((ans.a[u % k][v % k] == inf) ? -1 : ans.a[u % k][v % k]) << "\n";
    }
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    input();
    process();
    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...