This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1 << (x))
#define popcount __builtin_popcountll
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
return l + rd() % (r - l + 1);
}
const int N = 5e3 + 5;
const int mod = 1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int inf = 1e9; // INT_MAX;
const long long llinf = 1e18; // LLONG_MAX;
template<class X, class Y>
bool minimize(X &a, Y b) {
return a > b ? (a = b, true) : false;
}
template<class X, class Y>
bool maximize(X &a, Y b) {
return a < b ? (a = b, true) : false;
}
template<class X, class Y>
bool add(X &a, Y b) {
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
return true;
}
int n, m, k, q;
vector<pair<int, int> > adj[N];
int city[N];
typedef tuple<long long, int, int> node;
long long d[N][32];
void dijkstra() {
memset(d, 0x3f, sizeof d);
priority_queue<node, vector<node>, greater<node> > q;
for (int i = 1; i <= n; ++i) if (city[i]) {
q.push({0, i, 31});
d[i][31] = 0;
}
while (q.size()) {
long long u;
int du, mask;
tie(du, u, mask) = q.top(); q.pop();
if (du > d[u][mask])
continue;
for (pair<int, int> i : adj[u]) {
int v, c;
tie(v, c) = i;
if (d[u][mask] + c < d[v][mask]) {
d[v][mask] = d[u][mask] + c;
q.push({d[v][mask], v, mask});
}
for (int i = 1; i <= 5; ++i) if (getbit(mask, i - 1)) {
int newcost = 1LL * c * (10 - i) / 10;
int nmask = (mask ^ (1 << (i - 1)));
if (d[u][mask] + newcost < d[v][nmask]) {
d[v][nmask] = d[u][mask] + newcost;
q.push({d[v][nmask], v, nmask});
}
}
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp", "r",stdin);
// freopen(file".out", "w",stdout);
cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
int x;
cin >> x;
++x;
city[x] = 1;
}
for (int i = 1; i <= m; ++i) {
int u, v, c;
cin >> u >> v >> c;
++u, ++v;
adj[v].push_back({u, c});
}
dijkstra();
cin >> q;
while (q--) {
int s, p[6];
cin >> s;
++s;
for (int i = 1; i <= 5; ++i) {
cin >> p[i];
}
long long ans = d[s][31];
for (int mask = 0; mask < bit(5); ++mask) {
bool ok = true;
long long cur = d[s][mask];
for (int i = 1; i <= 5; ++i) if (getbit(mask, i - 1) == 0) {
if (p[i] == -1) {
cur = d[0][0];
break;
}
else {
cur += p[i];
}
}
if (ok) ans = min(ans, cur);
}
if (ans == d[0][0]) ans = -1;
cout << ans << '\n';
}
return 0;
}
# | 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... |