#include <bits/stdc++.h>
#define ll long long
#define task "votingcity"
using namespace std;
const int N = 5016;
const ll INF = 1e18;
int n, e, K, t[N], u, v, q;
ll w, p[N];
vector < pair <int, ll> > adj[N];
namespace sub8 {
ll d[(1 << 5) + 1][N];
priority_queue < pair <ll, pair <int, int> >, vector < pair <ll, pair <int, int> > >, greater < pair <ll, pair <int, int> > > > pq;
void solve() {
for (int mask = 0; mask < (1 << 5); mask++) {
for (int i = 0; i < n; i++) {
d[mask][i] = INF;
}
}
for (int i = 1; i <= K; i++) {
d[0][t[i]] = 0;
pq.push({d[0][t[i]], {t[i], 0}});
}
while (!pq.empty()) {
pair <ll, pair <int, int> > top = pq.top();
pq.pop();
ll dist = top.first;
int u = top.second.first;
int mask = top.second.second;
if (dist > d[mask][u]) {
continue;
}
for (auto v : adj[u]) {
int vertex = v.first;
ll value = v.second;
if (d[mask][vertex] > dist + value) {
d[mask][vertex] = dist + value;
pq.push({d[mask][vertex], {vertex, mask}});
}
for (int maskBit = ((1 << 5) - 1) ^ mask; maskBit > 0; maskBit ^= maskBit & -maskBit) {
int z = __builtin_ctz(maskBit & -maskBit) + 1;
int newMask = mask | (1 << (z - 1));
ll newVal = value - value / 10 * z;
if (d[newMask][vertex] > dist + newVal) {
d[newMask][vertex] = dist + newVal;
pq.push({d[newMask][vertex], {vertex, newMask}});
}
}
}
}
while (q--) {
int s;
cin >> s;
for (int i = 1; i <= 5; i++) {
cin >> p[i];
}
ll result = INF;
for (int mask = 0; mask < (1 << 5); mask++) {
bool flag = true;
ll cost = 0;
for (int i = 1; i <= 5; i++) {
if (mask >> (i - 1) & 1) {
if (p[i] == -1) {
flag = false;
break;
}
cost += p[i];
}
}
if (flag) {
result = min(result, d[mask][s] + cost);
}
}
if (result == INF) {
result = -1;
}
cout << result << "\n";
}
}
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> e >> K;
for (int i = 1; i <= K; i++) {
cin >> t[i];
}
for (int i = 1; i <= e; i++) {
cin >> u >> v >> w;
adj[v].push_back({u, w});
}
cin >> q;
sub8 :: solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |
| # | 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... |