Submission #861695

# Submission time Handle Problem Language Result Execution time Memory
861695 2023-10-16T16:41:18 Z wakandaaa Voting Cities (NOI22_votingcity) C++17
100 / 100
67 ms 8396 KB
#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
1 Correct 40 ms 3284 KB Output is correct
2 Correct 12 ms 1884 KB Output is correct
3 Correct 39 ms 3280 KB Output is correct
4 Correct 1 ms 1684 KB Output is correct
5 Correct 1 ms 1624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3284 KB Output is correct
2 Correct 12 ms 1884 KB Output is correct
3 Correct 39 ms 3280 KB Output is correct
4 Correct 1 ms 1684 KB Output is correct
5 Correct 1 ms 1624 KB Output is correct
6 Correct 42 ms 3356 KB Output is correct
7 Correct 12 ms 1872 KB Output is correct
8 Correct 42 ms 3288 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3284 KB Output is correct
2 Correct 12 ms 1884 KB Output is correct
3 Correct 39 ms 3280 KB Output is correct
4 Correct 1 ms 1684 KB Output is correct
5 Correct 1 ms 1624 KB Output is correct
6 Correct 42 ms 3356 KB Output is correct
7 Correct 12 ms 1872 KB Output is correct
8 Correct 42 ms 3288 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1624 KB Output is correct
11 Correct 59 ms 7536 KB Output is correct
12 Correct 15 ms 1884 KB Output is correct
13 Correct 51 ms 3364 KB Output is correct
14 Correct 1 ms 1628 KB Output is correct
15 Correct 1 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3284 KB Output is correct
2 Correct 12 ms 1884 KB Output is correct
3 Correct 39 ms 3280 KB Output is correct
4 Correct 1 ms 1684 KB Output is correct
5 Correct 1 ms 1624 KB Output is correct
6 Correct 3 ms 2140 KB Output is correct
7 Correct 12 ms 1884 KB Output is correct
8 Correct 44 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3284 KB Output is correct
2 Correct 12 ms 1884 KB Output is correct
3 Correct 39 ms 3280 KB Output is correct
4 Correct 1 ms 1684 KB Output is correct
5 Correct 1 ms 1624 KB Output is correct
6 Correct 42 ms 3356 KB Output is correct
7 Correct 12 ms 1872 KB Output is correct
8 Correct 42 ms 3288 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1624 KB Output is correct
11 Correct 3 ms 2140 KB Output is correct
12 Correct 12 ms 1884 KB Output is correct
13 Correct 44 ms 3284 KB Output is correct
14 Correct 40 ms 3284 KB Output is correct
15 Correct 12 ms 1884 KB Output is correct
16 Correct 47 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 4556 KB Output is correct
2 Correct 57 ms 7880 KB Output is correct
3 Correct 29 ms 2392 KB Output is correct
4 Correct 50 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2140 KB Output is correct
2 Correct 3 ms 2140 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 3 ms 2132 KB Output is correct
5 Correct 3 ms 2140 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 4 ms 2140 KB Output is correct
8 Correct 3 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3284 KB Output is correct
2 Correct 12 ms 1884 KB Output is correct
3 Correct 39 ms 3280 KB Output is correct
4 Correct 1 ms 1684 KB Output is correct
5 Correct 1 ms 1624 KB Output is correct
6 Correct 42 ms 3356 KB Output is correct
7 Correct 12 ms 1872 KB Output is correct
8 Correct 42 ms 3288 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1624 KB Output is correct
11 Correct 59 ms 7536 KB Output is correct
12 Correct 15 ms 1884 KB Output is correct
13 Correct 51 ms 3364 KB Output is correct
14 Correct 1 ms 1628 KB Output is correct
15 Correct 1 ms 1628 KB Output is correct
16 Correct 3 ms 2140 KB Output is correct
17 Correct 12 ms 1884 KB Output is correct
18 Correct 44 ms 3284 KB Output is correct
19 Correct 40 ms 3284 KB Output is correct
20 Correct 12 ms 1884 KB Output is correct
21 Correct 47 ms 3284 KB Output is correct
22 Correct 57 ms 4556 KB Output is correct
23 Correct 57 ms 7880 KB Output is correct
24 Correct 29 ms 2392 KB Output is correct
25 Correct 50 ms 3284 KB Output is correct
26 Correct 3 ms 2140 KB Output is correct
27 Correct 3 ms 2140 KB Output is correct
28 Correct 1 ms 1628 KB Output is correct
29 Correct 3 ms 2132 KB Output is correct
30 Correct 3 ms 2140 KB Output is correct
31 Correct 2 ms 1884 KB Output is correct
32 Correct 4 ms 2140 KB Output is correct
33 Correct 3 ms 2140 KB Output is correct
34 Correct 1 ms 1628 KB Output is correct
35 Correct 1 ms 1628 KB Output is correct
36 Correct 1 ms 1628 KB Output is correct
37 Correct 67 ms 8396 KB Output is correct
38 Correct 58 ms 7368 KB Output is correct
39 Correct 51 ms 3280 KB Output is correct
40 Correct 1 ms 1624 KB Output is correct
41 Correct 2 ms 1880 KB Output is correct
42 Correct 62 ms 6860 KB Output is correct
43 Correct 62 ms 6800 KB Output is correct
44 Correct 46 ms 3536 KB Output is correct
45 Correct 26 ms 2140 KB Output is correct