#include <bits/stdc++.h>
using namespace std;
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n, e, special;
cin >> n >> e >> special;
vector<bool> is_special(n, false);
for (int i = 0; i < special; i++) {
int x;
cin >> x;
is_special[x] = true;
}
vector<vector<pair<int, long long>>> g(n), rev_g(n);
for (int i = 0; i < e; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
rev_g[v].emplace_back(u, w);
}
vector<vector<long long>> dist(n, vector<long long>((1 << 5) + 1, 0));
vector<vector<bool>> visited(n, vector<bool>((1 << 5) + 1, 0));
priority_queue<pair<long long, pair<int, int>>, vector<pair<long long, pair<int, int>>>, greater<>> pq;
// totalCost, mask, node
for (int i = 0; i < n; i++)
if (is_special[i]) {
pq.push({0, {0, i}});
}
while (!pq.empty()) {
auto [totalCost, state] = pq.top();
pq.pop();
auto [mask, node] = state;
if (visited[node][mask]) continue;
visited[node][mask] = true;
dist[node][mask] = totalCost;
for (auto [v, w]: rev_g[node]) {
for (int bit = 0; bit < 5; bit++) {
bool isSet = mask & (1 << bit);
if (isSet) continue;
int newMask = mask | (1 << bit);
if (visited[v][newMask]) continue;
long long newPrice = (w * 10 - w * (bit + 1)) / 10 + totalCost;
if (!visited[v][newMask]) {
pq.push({newPrice, {newMask, v}});
}
}
}
for (auto [v, w]: rev_g[node]) {
if (visited[v][mask]) continue;
long long newPrice = w + totalCost;
pq.push({newPrice, {mask, v}});
}
}
int q;
cin >> q;
while (q--) {
int src;
cin >> src;
vector<int> ticket_price(5);
for (int i = 0; i < 5; i++) cin >> ticket_price[i];
long long ans = 1e16;
for (long long i = 0, tmp = 0; i < (1 << 5); i++) {
if (visited[src][i]) {
for (int j = 0; j < 5; j++) {
if (i & (1 << j)) {
if (ticket_price[j] == -1) {
tmp = 1e16;
break;
} else tmp += ticket_price[j];
}
}
ans = min(ans, tmp + dist[src][i]);
}
tmp = 0;
}
if (ans >= 1e16) cout << -1 << '\n';
else cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
5076 KB |
Output is correct |
2 |
Correct |
32 ms |
2908 KB |
Output is correct |
3 |
Correct |
84 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
5076 KB |
Output is correct |
2 |
Correct |
32 ms |
2908 KB |
Output is correct |
3 |
Correct |
84 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
77 ms |
5332 KB |
Output is correct |
7 |
Correct |
34 ms |
2904 KB |
Output is correct |
8 |
Correct |
79 ms |
5324 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
5076 KB |
Output is correct |
2 |
Correct |
32 ms |
2908 KB |
Output is correct |
3 |
Correct |
84 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
77 ms |
5332 KB |
Output is correct |
7 |
Correct |
34 ms |
2904 KB |
Output is correct |
8 |
Correct |
79 ms |
5324 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
106 ms |
9168 KB |
Output is correct |
12 |
Correct |
38 ms |
2908 KB |
Output is correct |
13 |
Correct |
85 ms |
5332 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
5076 KB |
Output is correct |
2 |
Correct |
32 ms |
2908 KB |
Output is correct |
3 |
Correct |
84 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
3164 KB |
Output is correct |
7 |
Correct |
33 ms |
2908 KB |
Output is correct |
8 |
Correct |
97 ms |
5220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
5076 KB |
Output is correct |
2 |
Correct |
32 ms |
2908 KB |
Output is correct |
3 |
Correct |
84 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
77 ms |
5332 KB |
Output is correct |
7 |
Correct |
34 ms |
2904 KB |
Output is correct |
8 |
Correct |
79 ms |
5324 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
3164 KB |
Output is correct |
12 |
Correct |
33 ms |
2908 KB |
Output is correct |
13 |
Correct |
97 ms |
5220 KB |
Output is correct |
14 |
Correct |
73 ms |
5328 KB |
Output is correct |
15 |
Correct |
33 ms |
2908 KB |
Output is correct |
16 |
Correct |
82 ms |
5340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
8596 KB |
Output is correct |
2 |
Correct |
99 ms |
7876 KB |
Output is correct |
3 |
Correct |
57 ms |
3552 KB |
Output is correct |
4 |
Correct |
89 ms |
5332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1492 KB |
Output is correct |
2 |
Correct |
9 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
8 ms |
1488 KB |
Output is correct |
5 |
Correct |
9 ms |
1488 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
1492 KB |
Output is correct |
8 |
Correct |
9 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
5076 KB |
Output is correct |
2 |
Correct |
32 ms |
2908 KB |
Output is correct |
3 |
Correct |
84 ms |
5160 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
77 ms |
5332 KB |
Output is correct |
7 |
Correct |
34 ms |
2904 KB |
Output is correct |
8 |
Correct |
79 ms |
5324 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
106 ms |
9168 KB |
Output is correct |
12 |
Correct |
38 ms |
2908 KB |
Output is correct |
13 |
Correct |
85 ms |
5332 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
3164 KB |
Output is correct |
17 |
Correct |
33 ms |
2908 KB |
Output is correct |
18 |
Correct |
97 ms |
5220 KB |
Output is correct |
19 |
Correct |
73 ms |
5328 KB |
Output is correct |
20 |
Correct |
33 ms |
2908 KB |
Output is correct |
21 |
Correct |
82 ms |
5340 KB |
Output is correct |
22 |
Correct |
88 ms |
8596 KB |
Output is correct |
23 |
Correct |
99 ms |
7876 KB |
Output is correct |
24 |
Correct |
57 ms |
3552 KB |
Output is correct |
25 |
Correct |
89 ms |
5332 KB |
Output is correct |
26 |
Correct |
10 ms |
1492 KB |
Output is correct |
27 |
Correct |
9 ms |
1492 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
8 ms |
1488 KB |
Output is correct |
30 |
Correct |
9 ms |
1488 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
9 ms |
1492 KB |
Output is correct |
33 |
Correct |
9 ms |
1748 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
98 ms |
8404 KB |
Output is correct |
38 |
Correct |
114 ms |
11716 KB |
Output is correct |
39 |
Correct |
82 ms |
5324 KB |
Output is correct |
40 |
Correct |
2 ms |
2396 KB |
Output is correct |
41 |
Correct |
3 ms |
2652 KB |
Output is correct |
42 |
Correct |
105 ms |
9028 KB |
Output is correct |
43 |
Correct |
118 ms |
11968 KB |
Output is correct |
44 |
Correct |
81 ms |
5332 KB |
Output is correct |
45 |
Correct |
55 ms |
3164 KB |
Output is correct |