194 lines
4.0 KiB
C++
194 lines
4.0 KiB
C++
/* Problem URL: https://codeforces.com/contest/1627/problem/E */
|
|
|
|
#include <bits/stdc++.h>
|
|
#include <ext/pb_ds/assoc_container.hpp>
|
|
#include <ext/pb_ds/tree_policy.hpp>
|
|
|
|
using namespace std;
|
|
using namespace __gnu_pbds;
|
|
|
|
template <class T, class comp = less<>>
|
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
|
|
|
#define V vector
|
|
|
|
#define rmin(a, b) a = min(a, b)
|
|
#define rmax(a, b) a = max(a, b)
|
|
|
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
|
|
|
#define repv(i, v) for (auto &i : (v))
|
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
|
#define sortv(v) sort(v.begin(), v.end())
|
|
#define all(v) (v).begin(), (v).end()
|
|
|
|
using vi = vector<int>;
|
|
using vvi = vector<vi>;
|
|
using vvvi = vector<vvi>;
|
|
using vvvvi = vector<vvvi>;
|
|
|
|
using ll = long long;
|
|
|
|
using vl = vector<ll>;
|
|
using vvl = vector<vl>;
|
|
using vvvl = vector<vvl>;
|
|
using vvvvl = vector<vvvl>;
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
|
os << vec[0];
|
|
for (size_t i = 1; i < vec.size(); i++) {
|
|
os << ' ' << vec[i];
|
|
}
|
|
os << '\n';
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
is >> i;
|
|
}
|
|
return is;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
|
for (auto &i : vec) {
|
|
os << i[0];
|
|
for (size_t j = 1; j < i.size(); j++) {
|
|
os << ' ' << i[j];
|
|
}
|
|
os << '\n';
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
for (auto &j : i) {
|
|
is >> j;
|
|
}
|
|
}
|
|
return is;
|
|
}
|
|
|
|
const int oo = INT32_MAX >> 1;
|
|
const ll OO = INT64_MAX >> 1;
|
|
|
|
|
|
void pre()
|
|
{
|
|
|
|
}
|
|
|
|
#define TEST 1
|
|
|
|
void solve()
|
|
{
|
|
int n, m, k;
|
|
cin >> n >> m >> k;
|
|
|
|
vl x(n);
|
|
cin >> x;
|
|
|
|
vvi var(n);
|
|
|
|
V<tuple<int, int, int, int, int>> edges;
|
|
while (k--) {
|
|
int a, b, c, d, e;
|
|
cin >> a >> b >> c >> d >> e;
|
|
a--, b--, c--, d--;
|
|
edges.emplace_back(a, b, c, d, e);
|
|
var[a].push_back(b);
|
|
var[c].push_back(d);
|
|
}
|
|
|
|
var[0].push_back(0);
|
|
|
|
rep(i, n) {
|
|
sortv(var[i]);
|
|
var[i].erase(unique(all(var[i])), var[i].end());
|
|
}
|
|
|
|
vvl dis(n);
|
|
V<V<V<tuple<int, int, ll>>>> graph(n);
|
|
rep(i, n) {
|
|
graph[i].resize(var[i].size());
|
|
dis[i].assign(var[i].size(), OO);
|
|
}
|
|
|
|
for (auto [a, b, c, d, e] : edges) {
|
|
b = lower_bound(all(var[a]), b) - var[a].begin();
|
|
d = lower_bound(all(var[c]), d) - var[c].begin();
|
|
|
|
graph[a][b].emplace_back(c, d, e);
|
|
}
|
|
|
|
priority_queue<tuple<int, ll, int>, V<tuple<int, ll, int>>, greater<>> pq;
|
|
dis[0][0] = 0;
|
|
pq.emplace(0, 0, 0);
|
|
|
|
while (!pq.empty()) {
|
|
auto [i, c, j] = pq.top();
|
|
pq.pop();
|
|
|
|
if (c > dis[i][j]) {
|
|
continue;
|
|
}
|
|
|
|
for (auto [u, v, w] : graph[i][j]) {
|
|
if (dis[u][v] > c - w) {
|
|
dis[u][v] = c - w;
|
|
pq.emplace(u, c - w, v);
|
|
}
|
|
}
|
|
|
|
if (j > 0) {
|
|
ll dd = (var[i][j] - var[i][j - 1]) * x[i];
|
|
if (dis[i][j - 1] > c + dd) {
|
|
dis[i][j - 1] = c + dd;
|
|
pq.emplace(i, c + dd, j - 1);
|
|
}
|
|
}
|
|
|
|
if (j < var[i].size() - 1) {
|
|
ll dd = (var[i][j + 1] - var[i][j]) * x[i];
|
|
if (dis[i][j + 1] > c + dd) {
|
|
dis[i][j + 1] = c + dd;
|
|
pq.emplace(i, c + dd, j + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
ll ans = OO;
|
|
rep(i, dis[n - 1].size()) {
|
|
int v = var[n - 1][i];
|
|
ll dd = (m - v - 1) * x[n - 1] + dis[n - 1][i];
|
|
rmin(ans, dd);
|
|
}
|
|
|
|
if (ans == OO) {
|
|
cout << "NO ESCAPE\n";
|
|
return;
|
|
}
|
|
|
|
cout << ans << '\n';
|
|
}
|
|
|
|
int main()
|
|
{
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
pre();
|
|
|
|
int t;
|
|
(TEST && cin >> t) || (t = 1);
|
|
while (t--) {
|
|
solve();
|
|
}
|
|
}
|