#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] , dp[N] , ord[Q];
vector<ll> g[N];
vector<ll> slow(){
vector<ll> ans;
for(ll i = 1; i <= q; i++){
for(ll j = 1; j <= n; j++){
dp[j] = INF;
}
dp[u[i]] = t[i];
set<pair<ll,ll>> st;
st.insert({t[i],u[i]});
while(st.size()){
ll ver = st.begin()->S;
st.erase(st.begin());
for(ll in : g[ver]){
ll to = (ver^b[in]^a[in]);
ll tim = dp[ver];
if(tim%s + l[in] > c[in]){
tim += s-tim%s + l[in];
} else {
tim += l[in];
}
if(dp[to] > tim){
st.erase({dp[to],to});
dp[to] = tim;
st.insert({dp[to],to});
}
}
}
ans.push_back(dp[v[i]]-t[i]);
}
return ans;
}
vector<ll> gocode(){
vector<ll> ans;
for(ll j = 1; j <= n; j++){
dp[j] = INF;
}
for(ll i = 1; i <= q; i++){
ord[i] = i;
ans.push_back(0);
}
sort(ord+1,ord+1+q,[&](int i , int j){
return t[i] > t[j];
});
for(int _ = 1; _ <= q; _++){
int i = ord[_];
dp[u[i]] = t[i];
set<pair<ll,ll>> st;
st.insert({t[i],u[i]});
while(st.size()){
ll ver = st.begin()->S;
st.erase(st.begin());
for(ll in : g[ver]){
ll to = (ver^b[in]^a[in]);
ll tim = dp[ver];
if(tim%s + l[in] > c[in]){
tim += s-tim%s + l[in];
} else {
tim += l[in];
}
if(dp[to] > tim){
st.erase({dp[to],to});
dp[to] = tim;
st.insert({dp[to],to});
}
}
}
ans[i-1] = dp[v[i]]-dp[u[i]];
}
return ans;
}
vector<long long> solve(){
int cnt = 0;
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]++;
if(u[i] == 1){
cnt++;
}
}
if(cnt == q){
return gocode();
} else{
return slow();
}
}
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";
// }
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |