#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() {
while (q--) {
int s;
cin >> s;
for (int i = 1; i <= 5; i++) {
cin >> p[i];
}
int initial = 0;
for (int i = 1; i <= 5; i++) {
if (p[i] == -1) {
continue;
}
initial |= (1 << (i - 1));
}
/*
for (int mask = 0; mask < (1 << 5); mask++) {
for (int i = 0; i < n; i++) {
d[mask][i] = INF;
}
}
*/
for (int i = 0; i < n; i++) {
d[initial][i] = INF;
}
for (int submask = initial; submask > 0; submask = (submask - 1) & initial) {
for (int i = 0; i < n; i++) {
d[submask][i] = INF;
}
}
for (int i = 0; i < n; i++) {
d[0][i] = INF;
}
for (int i = 1; i <= K; i++) {
d[initial][t[i]] = 0;
pq.push({d[initial][t[i]], {t[i], initial}});
}
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 = mask; maskBit > 0; maskBit ^= maskBit & -maskBit) {
int z = __builtin_ctz(maskBit & -maskBit) + 1;
ll newVal = value - value / 10 * z + p[z];
int newMask = mask ^ (1 << (z - 1));
if (d[newMask][vertex] > dist + newVal) {
d[newMask][vertex] = dist + newVal;
pq.push({d[newMask][vertex], {vertex, newMask}});
}
}
}
}
ll result = INF;
result = min(result, d[initial][s]);
for (int submask = initial; submask > 0; submask = (submask - 1) & initial) {
result = min(result, d[submask][s]);
}
result = min(result, d[0][s]);
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:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | 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... |