# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1157940 | 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 = 1e18 , 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];
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});
}
}
}
if(dp[B] != INF) return 1;
return 0;
}
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]++;
}
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});
}
}
}
}
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= n; j++){
ll l = 0 , r = INF;
res[i][j] = 0;
while(l <= r){
ll md = (l+r) >> 1;
if(check(i,j,md)){
res[i][j] = md;
l = md+1;
} else {
r = md-1;
}
}
}
}
vector<ll> ans;
for(ll i = 1; i <= q; i++){
ll cur = INF;
for(ll to = 1; to <= n; to++){
if(res[u[i]][to] >= t[i]) cur = min(cur , s+dp0[to][v[i]]-t[i]);
}
ans.push_back(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";
}
}