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;
#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif
#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int N, M;
cin >> N >> M;
vi T(M), L(M), R(M), C(M);
for (int i = 0; i < M; i++) cin >> T[i] >> L[i] >> R[i] >> C[i], L[i]--;
vector<i64> dist(M, -1);
typedef pair<i64, int> U;
priority_queue<U, vector<U>, greater<U>> pq;
for (int i = 0; i < M; i++) if (L[i] == 0) pq.push({dist[i] = C[i], i});
while (sz(pq)) {
auto [d, i] = pq.top(); pq.pop();
for (int j = 0; j < M; j++) if (dist[j] == -1) {
if (T[i] <= T[j] && L[j] + T[j] <= R[i] + T[i])
pq.push({dist[j] = dist[i] + C[j], j});
if (T[j] < T[i] && R[i] - T[i] >= L[j] - T[j])
pq.push({dist[j] = dist[i] + C[j], j});
}
}
i64 ans = LLONG_MAX;
for (int i = 0; i < M; i++) if (~dist[i] && R[i] == N)
ans = min(ans, dist[i]);
cout << (ans == LLONG_MAX ? -1 : ans) << '\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... |