제출 #1318632

#제출 시각아이디문제언어결과실행 시간메모리
1318632vaishakhvToll (BOI17_toll)C++20
0 / 100
193 ms145524 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define eb emplace_back // faster than push_back xD

// pbds UwU
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define oset tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> // use pair for ms

// my io library :D
#define m1(x) template<class T, class... U> void x(T&& a, U&&... b)
#define m2(x) (ll[]){(x forward<U>(b),0)...}

m1(pr){cout << forward<T>(a);  m2(cout << " " <<); cout << "\n";}
m1(re){cin >> forward<T>(a); m2(cin >>);}

const ll cap = 5e4+1;
ll dist[cap], k;
vector<pair<ll,ll>> adj[cap];
vector<tuple<ll,ll,ll>> adj_by_layer[cap];

/*
void dijkstra(ll s){
    priority_queue<pair<ll,ll>> pq;
    fill(dist, dist+cap, 1e18);
    pq.push({0, s});
    dist[s] = 0;

    while (!pq.empty()){
        pair<ll,ll> oknext = pq.top(); pq.pop();
        oknext.first *= -1;
        for (auto u: adj[oknext.second]){
            if (dist[oknext.second] + u.second < dist[u.first]){
                dist[u.first] = dist[oknext.second] + u.second;
                pq.push({-dist[u.first], u.first});
            }    
        }   
    }
}
*/

ll layer(ll a){
    return a/k;
}

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

    ll n, m, o;
    re(k, n, m, o);

    for (ll i{}; i < m; i++){
        ll a, b, t; re(a, b, t);
        adj[a].eb(make_pair(b, t));
        adj_by_layer[layer(a)].eb(make_tuple(a,b,t));
    }

    ll nolayers = (n+k-1)/k;
    ll lastsz = n - k*(nolayers-1), log = ceil(log2(nolayers)) + 1; 

    vector<vector<vector<vector<ll>>>> up(nolayers, vector<vector<vector<ll>>>(32, vector<vector<ll>>(k, vector<ll>(k, 1e18))));

    for (ll i{}; i < nolayers; i++) {
        for (auto tup :adj_by_layer[i]) {
            ll a, b, t; tie(a, b, t) = tup;
            ll x = a % k, y = b % k;
            up[i][0][x][y] = min(up[i][0][x][y], t);
        }
    }


    for (ll i{}; i < nolayers; i++){
        for (ll j = 1; j < log; j++){
            if (i + (1LL<<j) >= nolayers) continue;
            for (ll x{}; x < k; x++){
                for (ll y{}; y < k; y++){                    
                    for (ll mid{}; mid < k; mid++){
                        ll a = up[i][j-1][x][mid];
                        ll b = up[(1LL<<(j-1)) + i][j-1][mid][y];
                        if (a == (ll)1e18 || b == (ll)1e18) continue;
                        up[i][j][x][y] = min(up[i][j][x][y], a + b);
                    }
                }
            }
        }
    }

    for (ll i{}; i < o; i++){
        ll a, b; re(a, b);
        ll la = layer(a), lb = layer(b);
        ll diff = lb - la, curl = la;
        vector<ll> cur(k, 1e18);
        cur[a%k] = 0;
        
        ll ans = 0;
        for (ll j{}; j < log; j++){
            if (diff & ((1LL<<j)) && curl + (1LL<<j) <= lb){
                vector<ll> nex(k, 1e18);
                for (ll x{}; x < k; x++){
                    for (ll y{}; y < k; y++){
                        if (cur[x] == (ll)1e18 || up[curl][j][x][y] == (ll)1e18) continue;
                        nex[y] = min(nex[y], cur[x] + up[curl][j][x][y]);
                    }
                }
                cur = nex;
                curl += ((1LL<<j));
            }            
        }

        ans = cur[b%k];
        if (ans >= (ll)1e18) pr(-1);
        else pr(ans);
    }
}
#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...