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 <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const ll Inf = 1e18;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector <int> t(m + 1), l(m + 1), r(m + 1), c(m + 2);
for (int i = 1; i <= m; ++i) {
cin >> t[i] >> l[i] >> r[i] >> c[i];
--l[i];
}
vector <vector <int>> g(m + 2);
for (int i = 1; i <= m; ++i) {
if (l[i] == 0) {
g[0].push_back(i);
}
if (r[i] == n) {
g[i].push_back(m + 1);
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= m; ++j) {
if (i == j) continue;
if (t[i] == t[j]) {
int cl = max(l[i], l[j]);
int cr = min(r[i], r[j]);
if (cl <= cr) {
g[i].push_back(j);
g[j].push_back(i);
}
} else if (t[i] < t[j]) {
int tm = t[j] - t[i];
int cl = max(l[i] + tm, l[j]);
int cr = min(r[i] - tm, r[j]);
if (cl <= cr) {
g[i].push_back(j);
g[j].push_back(i);
}
} else {
int tm = t[i] - t[j];
int cl = max(l[i], l[j] + tm);
int cr = min(r[i], r[j] - tm);
if (cl <= cr) {
g[i].push_back(j);
g[j].push_back(i);
}
}
}
}
vector <ll> d(m + 2, Inf);
d[0] = 0;
vector <int> used(m + 2);
while (true) {
int id = -1;
for (int u = 0; u < m + 2; ++u) {
if (!used[u] && (id == -1 || d[u] < d[id])) {
id = u;
}
}
if (id == -1) break;
used[id] = 1;
for (auto v : g[id]) {
if (d[v] > d[id] + c[v]) {
d[v] = d[id] + c[v];
}
}
}
if (d[m + 1] == Inf) {
cout << "-1\n";
} else {
cout << d[m + 1] << '\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... |