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;
#define LCBorz ios_base::sync_with_stdio(false);cin.tie(0);
#define int long long
#define all(x) x.begin(), x.end()
#define x first
#define y second
const int N=150005;
const int INF=1e18;
struct st{
int t,l,r,c;
};
int32_t main() {
int m,n;cin>>m>>n;
vector<st> v(n);
for(int i=0;i<n;i++){
int a,b,c,d;cin>>a>>b>>c>>d;
v[i]={a,b,c,d};
}
vector<int> dis(n,INF);
priority_queue<pair<int,int>> pq;
for(int i=0;i<n;i++){
if(v[i].l==1){
dis[i]=v[i].c;
pq.push({-v[i].c,i});
}
}
while(pq.size()){
auto [d,i]=pq.top();
pq.pop();
d=-d;
if(d!=dis[i]){
continue;
}
for(int j=0;j<n;j++){
if(j==i){
continue;
}
if(abs(v[i].t-v[j].t)<=v[i].r-v[j].l+1){
if(dis[j]>dis[i]+v[j].c){
dis[j]=dis[i]+v[j].c;
pq.push({-dis[j],j});
}
}
}
}
int ans=INF;
for(int i=0;i<n;i++){
//cout<<dis[i]<<endl;
if(v[i].r==m){
ans=min(ans,dis[i]);
}
}
cout<<(ans==INF?-1:ans)<<endl;
return 0;
}
/*
10 5
1 5 10 3
1 1 6 1
1 2 8 3
1 6 10 4
1 1 3 1
*/
# | 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... |