/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
const ll INF = 1e18;
int n, m;
array<ll, 4> a[N];
ll dp[N];
vi g[N];
bool check(int i, int j){ // i to j
return abs(a[i][2] - a[j][2]) <= a[i][1] - a[j][0] + 1;
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= m; ++i){
cin >> a[i][2] >> a[i][0] >> a[i][1] >> a[i][3];
}
ll ans = INF;
vector<ll> dist(m + 1, INF);
for(int i = 1; i <= m; ++i){
for(int j = 1; j <= m; ++j){
if(i != j && check(i, j)){
g[i].pb(j);
}
}
}
for(int i = 1; i <= m; ++i){
if(a[i][0] == 1){
dist[i] = a[i][3];
}
}
int cnt = 0;
vector<bool> vis(m + 1);
while(cnt < m){
int v = -1;
for(int i = 1; i <= m; ++i){
if(!vis[i] && (v == -1 || dist[v] > dist[i])){
v = i;
}
}
vis[v] = 1;
++cnt;
for(int u: g[v]){
dist[u] = min(dist[u], dist[v] + a[u][3]);
}
}
for(int i = 1; i <= m; ++i){
if(a[i][1] == n){
ans = min(ans, dist[i]);
}
}
if(ans==INF){
cout << -1;
}else{
cout << ans;
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
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... |