# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1158447 | Kaztaev_Alisher | Escape Route (JOI21_escape_route) | C++20 | 0 ms | 0 KiB |
#include "escape_route.h"
#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
using namespace std;
using ll = long long;
const ll N = 95 , M = 5005 , Q = 3e6+5 , inf = 2e9 + 7;
const ll INF = 1e16 , mod = 1e9+7;
ll n , m , s , q , a[M] , b[M] , l[M] , c[M] , u[Q] , v[Q] , t[Q] , ord[Q] , dp0[N][N] , res[N][N] , dp[N];
vector<ll> g[N] , que[N][N];
ll check(ll A , ll B , ll C){
for(ll i = 1; i <= n; i++) dp[i] = INF;
dp[A] = C;
set<pair<ll,ll>> st;
st.insert({dp[A],A});
while(st.size()){
ll v = st.begin()->S;
st.erase(st.begin());
for(ll in : g[v]){
ll to = (v^b[in]^a[in]);
ll tim = dp[v] + l[in];
if(dp[to] > tim && tim <= c[in]){
st.erase({dp[to],to});
dp[to] = tim;
st.insert({dp[to],to});
}
}
}
return dp[B];
}
void kald(){
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= n; j++) dp0[i][j] = INF;
set<pair<ll,ll>> st;
dp0[i][i] = 0;
st.insert({0,i});
while(st.size()){
ll v = st.begin()->S;
st.erase(st.begin());
for(ll in : g[v]){
ll to = (v^b[in]^a[in]);
ll tim = dp0[i][v];
if(tim%s + l[in] > c[in]){
tim += s-tim%s + l[in];
} else {
tim += l[in];
}
if(dp0[i][to] > tim){
st.erase({dp0[i][to],to});
dp0[i][to] = tim;
st.insert({dp0[i][to],to});
}
}
}
}
}
void can(){
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= n; j++){
if(i == j) {
res[i][j] = INF;
continue;
}
ll l = 0 , r = s;
res[i][j] = 0;
while(l <= r){
ll md = (l+r) >> 1;
if(check(i,j,md) != INF){
res[i][j] = md;
l = md+1;
} else {
r = md-1;
}
}
}
}
}
vector<long long> solve(){
for(ll i = 1; i <= m; i++){
a[i]++; b[i]++;
g[a[i]].push_back(i);
g[b[i]].push_back(i);
}
for(ll i = 1; i <= q; i++) {
u[i]++; v[i]++;
}
kald();
can();
vector<ll> ans;
for(ll i = 1; i <= q; i++){
que[u[i]][v[i]].push_back(i);
ans.push_back(0);
}
for(ll u = 1; u <= n; u++){
for(ll v = 1; v <= n; v++){
ll cur = dp0[u][v] , nxt = 0;
vector<pair<ll,ll>> vec;
vec.push_back({0,cur});
// ll k = 2;
while(1){
ll l = nxt+1, r = s;
// if(u == 2 && v == 3) cout << nxt <<" " << cur << "\n";
ll prosh = nxt;
while(l <= r){
ll md = (l+r) >> 1;
if(check(u,v,md)-md == cur){
nxt = md+1;
l = md+1;
} else {
r = md-1;
}
}
if(nxt == prosh){
vec.push_back({nxt+1 , INF});
break;
}
cur = check(u,v,nxt);
// if(u == 2 && v == 3) cout << nxt <<" " << cur << "\n";
vec.push_back({nxt,cur});
if(cur == INF) break;
}
for(ll i : que[u][v]){
ll l = 0, r = vec.size()-1 , time = 0;
while(l <= r){
ll md = (l+r) >> 1;
if(vec[md].F <= t[i]){
time = md;
l = md+1;
} else {
r = md-1;
}
}
// cout << vec[time].F <<" " << t[i] << "\n";
if(vec[time].S != INF){
ans[i-1] = vec[time].S-vec[time].F;
} else {
ll cur = INF;
for(ll to = 1; to <= n; to++){
if(res[u][to] >= t[i]) {
cur = min(cur , s+dp0[to][v]-t[i]);
// cout << s + dp0[to][v] - t[i] <<" " << to << "\n";
}
}
ans[i-1] = cur;
}
}
}
}
return ans;
}
vector<long long> calculate_necessary_time(int N, int M, long long S, int Q, vector<int> A, vector<int> B, vector<long long> L, vector<long long> C, vector<int> U, vector<int> V, vector<long long> T) {
n = N;
m = M;
q = Q;
s = S;
for(int i = 0; i < m; i++){
a[i+1] = A[i];
b[i+1] = B[i];
c[i+1] = C[i];
l[i+1] = L[i];
}
for(int i = 0; i < q; i++){
u[i+1] = U[i];
v[i+1] = V[i];
t[i+1] = T[i];
}
return solve();
}
int main(){
cin >> n >> m >> s >> q;
for(int i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> l[i] >> c[i];
}
for(int i = 1; i <= q; i++){
cin >> u[i] >> v[i] >> t[i];
}
vector<ll> x = solve();
for(ll y : x){
cout << y <<"\n";
}
}