이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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> a(n), b(n), c(n), d(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
vector <ll> dpL(n, Inf), dpR(n, Inf);
for (int i = 0; i < n; ++i) {
if (a[i] == 1) {
dpL[i] = d[i];
}
for (int j = i - 1; j >= 0; --j) {
if (a[i] <= c[j] && c[j] <= b[i]) {
dpL[i] = min(dpL[i], dpL[j] + d[i]);
}
}
}
for (int i = 0; i < n; ++i) {
if (b[i] == m) {
dpR[i] = d[i];
}
for (int j = i - 1; j >= 0; --j) {
if (a[i] <= c[j] && c[j] <= b[i]) {
dpR[i] = min(dpR[i], dpR[j] + d[i]);
}
}
}
ll ans = Inf;
for (int i = 0; i < n; ++i) {
ll costL = Inf, costR = Inf;
for (int j = i - 1; j >= 0; --j) {
if (a[i] <= c[j] && c[j] <= b[i]) {
costL = min(costL, dpL[j]);
costR = min(costR, dpR[j]);
}
}
if (a[i] == 1) costL = 0;
if (b[i] == m) costR = 0;
ans = min(ans, costL + costR + d[i]);
}
if (ans != Inf) cout << ans << '\n';
else cout << "-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... |