# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
714847 | ajab_01 | Toll (BOI17_toll) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 9;
const int N = 5e4 + 5;
int SQ = 250;
vector<pair<int , int> > g[N];
int dp[N][SQ] , dis[N] , k , n , m , o , a , b;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> k >> n >> m >> o;
for(int i = 0 ; i < m ; i++){
int u , v , w;
cin >> u >> v >> w;
g[v].push_back({u , w});
}
for(int i = 0 ; i < n ; i++){
for(int d = 1 ; d < min(n - i , SQ) ; d++){
int v = i + d;
dp[i][d] = INF;
for(pair<int , int> p : g[v]){
int u = p.first , w = p.second;
if(u < i) continue;
dp[i][d] = min(dp[i][d] , dp[i][u - i] + w);
}
}
}
SQ = 69 + 85 + 1;
for(int oo = 0 ; oo < o ; oo++){
cin >> a >> b;
int vala = a / k , valb = b / k;
fill(dis + vala * k , dis + (vala + 1) * k , INF);
dis[a] = 0;
while(valb - vala >= SQ){
fill(dis + (vala + SQ - 1) * k , dis + (vala + SQ) * k , INF);
for(int i = (vala + SQ - 1) * k ; i < (vala + SQ) * k ; i++) for(int j = vala * k ; j < (vala + 1) * k ; j++) dis[i] = min(dis[i] , dis[j] + dp[j][i - j]);
}
dis[b] = INF;
for(int i = vala * k ; i < (vala + 1) * k ; i++) dis[b] = min(dis[b] , dis[i] + dp[i][b - i]);
(dis[b] == INF ? cout << -1 << '\n' : cout << dis[b] << '\n');
}
return 0;
}