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>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 1e5 + 5;
int N, M;
int T[MAX], L[MAX], R[MAX], C[MAX], rnk[MAX];
bool vis[MAX];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> adj1[MAX];
priority_queue<pair<int, int>> adj2[MAX];
ll f[MAX];
void solve(){
cin >> N >> M;
vector<int> v;
for(int i=1; i<=M; i++){
cin >> T[i] >> L[i] >> R[i] >> C[i];
v.push_back(T[i]);
}
cps(v);
for(int i=1; i<=M; i++){
int p = lower_bound(ALL(v), T[i]) - v.begin() + 1;
rnk[i] = p;
for(int k=p; k>0; k-=k&(-k)){
adj1[k].push(make_pair(L[i] + T[i], i));
}
for(int k=p; k<=sz(v); k+=k&(-k)){
adj2[k].push(make_pair(T[i] - L[i], i));
}
}
// //Dijkstra
memset(f, 0x3f, sizeof f);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
for(int i=1; i<=N; i++){
if(L[i] == 1){
pq.push(make_pair(C[i], i));
f[i] = C[i];
}
}
while(sz(pq)){
auto u = pq.top();
// cout << u << ' ' << c << '\n';
pq.pop();
if(u.fi > f[u.se]) continue;
int k = rnk[u.se];
for(int p=k; p<=sz(v); p+=p&(-p)){
while(sz(adj1[p])){
auto v = adj1[p].top();
if(v.fi > R[u.se] + T[u.se] + 1) break;
adj1[p].pop();
if(minimize(f[v.se], u.fi + C[v.se])){
pq.push(make_pair(f[v.se], v.se));
}
}
}
for(int p=k; p>0; p-=p&(-p)){
while(sz(adj2[p])){
auto v = adj2[p].top();
if(v.fi < T[u.se] - R[u.se] - 1) break;
adj2[p].pop();
if(minimize(f[v.se], u.fi + C[v.se])){
pq.push(make_pair(f[v.se], v.se));
}
}
}
}
ll ans = oo;
for(int i=1; i<=N; i++) if(R[i] == N){
minimize(ans, f[i]);
}
if(ans == oo) cout << -1 << '\n';
else cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("deggraph.inp", "r", stdin);
// freopen("deggraph.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
# | 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... |