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>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b or a == -1) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;
struct edge {
int a, b, x, y, c;
edge () {};
edge (int _a, int _b, int _x, int _y, int _c) {
a = _a, b = _b, x = _x, y = _y, c = _c;
}
};
struct meals {
int l, r;
bool operator < (const meals & other) {
return l < other.l;
}
} meal[N];
edge e[N];
int n, m, w, land[N];
vector<int> g[N];
namespace SUB1 {
const int W = 10;
const int M = (int)1e3 + 7;
long long dp[M][1 << W];
using pli = pair<long long, ii>;
int ov[M];
long long sol() {
memset(dp, -1, sizeof dp);
priority_queue<pli, vec<pli>, greater<pli>> pq;
dp[m][0] = 0; pq.push(_mp(0, _mp(m, 0)));
auto intersect = [](int x, int y, int u, int v) -> bool {
return !(x > v or y < u);
};
for(int i = 0; i < m; ++i) {
for(int j = 0; j < w; ++j) {
if (intersect(e[i].a, e[i].b, meal[j].l, meal[j].r)) {
ov[i] |= (1 << j);
}
}
}
while (!pq.empty()) {
auto cur = pq.top(); pq.pop();
int ed = cur.se.fi;
int mask = cur.se.se | ov[ed];
if (cur.fi != dp[ed][mask]) continue ;
int u = e[ed].y;
int tx = e[ed].b;
for(int t = 0; t < w; ++t) if ( !(1 & mask >> t) ) {
if (meal[t].l > e[ed].b and mini(dp[ed][mask ^ (1 << t)], cur.fi + land[u])) {
pq.push(_mp(cur.fi + land[u], _mp(ed, mask ^ (1 << t))));
}
} else
tx = max(tx, meal[t].l);
for(int id : g[u]) {
if (tx <= e[id].a) {
if (mini(dp[id][mask], cur.fi + e[id].c)) {
pq.push(_mp(dp[id][mask], _mp(id, mask)));
}
}
}
}
long long ans = -1;
for(int i = 0; i < m; ++i) {
if (e[i].y == n - 1) {
for(int mask = 0; mask < (1 << w); ++mask) if (dp[i][mask] != -1) {
mini(dp[i][mask | ov[i]], dp[i][mask]);
}
if (dp[i][(1 << w) - 1] != -1) {
mini(ans, dp[i][(1 << w) - 1]);
}
}
}
return ans;
}
};
long long solve(
int N, int M, int W,
vector<int> T, vector<int> X, vector<int> Y,
vector<int> A, vector<int> B, vector<int> C, vector<int> L, vector<int> R) {
n = N, m = M, w = W;
for(int i = 0; i < n; ++i) land[i] = T[i];
for(int i = 0; i < m; ++i) {
e[i] = edge(A[i], B[i], X[i], Y[i], C[i]);
g[X[i]].push_back(i);
}
e[m] = edge(0, 0, 0, 0, 0);
for(int i = 0; i < w; ++i) {
meal[i].l = L[i], meal[i].r = R[i];
}
if (w <= 10 and m <= 1000) {
return SUB1::sol();
}
return -1;
}
#ifdef LOCAL
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
int N, M, W;
assert(3 == scanf("%d %d %d", &N, &M, &W));
std::vector<int> t(N);
std::vector<int> x(M);
std::vector<int> y(M);
std::vector<int> a(M);
std::vector<int> b(M);
std::vector<int> c(M);
std::vector<int> l(W);
std::vector<int> r(W);
for (int i = 0; i < N; i++)
assert(1 == scanf("%d", &t[i]));
for (int i = 0; i < M; i++)
assert(5 == scanf("%d %d %d %d %d", &x[i], &y[i], &a[i], &b[i], &c[i]));
for (int i = 0; i < W; i++)
assert(2 == scanf("%d %d", &l[i], &r[i]));
printf("%lld", solve(N, M, W, t, x, y, a, b, c, l, r));
}
#endif LOCAL
Compilation message (stderr)
train.cpp:163:8: warning: extra tokens at end of #endif directive [-Wendif-labels]
163 | #endif LOCAL
| ^~~~~
# | 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... |