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 MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
template <class num_t>
struct Dijkstra {
int n; bool undirected;
const num_t INF = (num_t) 1e18;
vector <vector <pair <int, num_t>>> adj;
vector <num_t> dist;
priority_queue <pair <num_t, int>, vector <pair <num_t, int>>, greater <pair <num_t, int>>> q;
Dijkstra(int _n = 0, bool undirected = true) : undirected(undirected) {
resize(_n);
}
void resize(int _n) {
n = _n;
dist.resize(n + 1);
adj.assign(n + 1, vector <pair <int, num_t>>{});
}
void addedges(int u, int v, num_t w) {
adj[u].emplace_back(v, w);
if(undirected) adj[v].emplace_back(u, w);
}
num_t min_dist(int source, int sink) {
fill(dist.begin(), dist.end(), INF);
q.emplace(dist[source] = 0, source);
while(!q.empty()) {
auto [du, u] = q.top(); q.pop();
if(dist[u] != du) continue;
for (auto [v, w] : adj[u]) if(dist[v] > du + w) {
dist[v] = du + w;
q.emplace(dist[v], v);
}
}
return dist[sink];
}
};
const int MAXN = 1e5 + 5;
struct Data {
int T, L, R, C;
} A[MAXN];
int N, M;
long long dp[MAXN];
void you_make_it(void) {
cin >> N >> M;
Dijkstra <long long> T(M + 1, false);
FOR(i, 1, M) {
cin >> A[i].T >> A[i].L >> A[i].R >> A[i].C;
if(A[i].L == 1) T.addedges(0, i, A[i].C);
if(A[i].R == N) T.addedges(i, M + 1, 0);
}
FOR(i, 1, M) FOR(j, 1, M) if(i != j) {
if(A[i].R - abs(A[i].T - A[j].T) + 1 >= A[j].L) {
T.addedges(i, j, A[j].C);
}
}
long long ans = T.min_dist(0, M + 1);
cout << (ans >= 1E18 ? -1 : ans);
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
# | 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... |