-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path766e.cpp
More file actions
99 lines (83 loc) · 2.3 KB
/
766e.cpp
File metadata and controls
99 lines (83 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
void solve() {
ll n, m, k;
cin >> n >> m >> k;
vi cost(n);
for (ll i = 0; i < n; i++) {
cin >> cost[i];
}
map<pi, array<ll, 3>> ed;
vector<set<pi>> lad(n);
vi b(n);
for (ll i = 0; i < k; i++) {
ll x, y, z, w, c;
cin >> x >> y >> z >> w >> c;
x--, y--, z--, w--;
ed[{x, y}] = {z, w, -c};
lad[x].insert({y, 1});
lad[z].insert({w, 0});
}
lad[n - 1].insert({m - 1, 0});
priority_queue<array<ll, 4>> pq;
pq.push({0, 0, 0, 0});
vector<vector<array<ll, 2>>> vis(n, vector<array<ll, 2>>(m, array<ll, 2>{0, 0}));
vis[0][0][0] = true;
ll ans = 1e9;
bool pos = 0;
while (!pq.empty()) {
array<ll, 4> top = pq.top();
pq.pop();
ll x = top[1];
ll y = top[2];
ll dist = -top[0];
ll tp = top[3];
if (x == n - 1 && y == m - 1) {
ans = dist;
pos = 1;
break;
}
auto it = lad[x].find({y, tp});
if (it != lad[x].begin()) {
it--;
if (!vis[x][(*it).first][(*it).second]) {
pq.push({-(dist + abs(y - (*it).first) * cost[x]), x, (*it).first, (*it).second});
vis[x][(*it).first][(*it).second] = true;
}
it++;
}
if (it != lad[x].end()) {
it++;
if (it != lad[x].end()) {
if (!vis[x][(*it).first][(*it).second]) {
pq.push({-(dist + abs(y - (*it).first) * cost[x]), x, (*it).first, (*it).second});
vis[x][(*it).first][(*it).second] = true;
}
}
it--;
}
if (tp == 1) {
if (!vis[ed[{x, y}][0]][ed[{x, y}][1]][0LL]) {
pq.push({-(dist + ed[{x, y}][2]), ed[{x, y}][0], ed[{x, y}][1], 0LL});
vis[ed[{x, y}][0]][ed[{x, y}][1]][0LL] = true;
}
}
}
if (!pos)
cout << "NO ESCAPE" << endl;
else
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
while (t--) solve();
return 0;
}