Submission #873926

# Submission time Handle Problem Language Result Execution time Memory
873926 2023-11-16T02:47:57 Z noiaint Voting Cities (NOI22_votingcity) C++17
100 / 100
77 ms 8288 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 42 ms 3324 KB Output is correct
2 Correct 13 ms 2096 KB Output is correct
3 Correct 46 ms 3148 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 3324 KB Output is correct
2 Correct 13 ms 2096 KB Output is correct
3 Correct 46 ms 3148 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1604 KB Output is correct
6 Correct 47 ms 3304 KB Output is correct
7 Correct 15 ms 2080 KB Output is correct
8 Correct 45 ms 3284 KB Output is correct
9 Correct 1 ms 1776 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 3324 KB Output is correct
2 Correct 13 ms 2096 KB Output is correct
3 Correct 46 ms 3148 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1604 KB Output is correct
6 Correct 47 ms 3304 KB Output is correct
7 Correct 15 ms 2080 KB Output is correct
8 Correct 45 ms 3284 KB Output is correct
9 Correct 1 ms 1776 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
11 Correct 63 ms 8032 KB Output is correct
12 Correct 16 ms 2112 KB Output is correct
13 Correct 57 ms 3292 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 42 ms 3324 KB Output is correct
2 Correct 13 ms 2096 KB Output is correct
3 Correct 46 ms 3148 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1604 KB Output is correct
6 Correct 3 ms 2140 KB Output is correct
7 Correct 13 ms 1956 KB Output is correct
8 Correct 51 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 3324 KB Output is correct
2 Correct 13 ms 2096 KB Output is correct
3 Correct 46 ms 3148 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1604 KB Output is correct
6 Correct 47 ms 3304 KB Output is correct
7 Correct 15 ms 2080 KB Output is correct
8 Correct 45 ms 3284 KB Output is correct
9 Correct 1 ms 1776 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
11 Correct 3 ms 2140 KB Output is correct
12 Correct 13 ms 1956 KB Output is correct
13 Correct 51 ms 3284 KB Output is correct
14 Correct 43 ms 3288 KB Output is correct
15 Correct 12 ms 2108 KB Output is correct
16 Correct 51 ms 3316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 4356 KB Output is correct
2 Correct 62 ms 8072 KB Output is correct
3 Correct 32 ms 2712 KB Output is correct
4 Correct 51 ms 3244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2140 KB Output is correct
2 Correct 3 ms 2144 KB Output is correct
3 Correct 1 ms 1636 KB Output is correct
4 Correct 3 ms 2144 KB Output is correct
5 Correct 4 ms 2152 KB Output is correct
6 Correct 2 ms 1892 KB Output is correct
7 Correct 4 ms 2152 KB Output is correct
8 Correct 3 ms 2112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 3324 KB Output is correct
2 Correct 13 ms 2096 KB Output is correct
3 Correct 46 ms 3148 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 1 ms 1604 KB Output is correct
6 Correct 47 ms 3304 KB Output is correct
7 Correct 15 ms 2080 KB Output is correct
8 Correct 45 ms 3284 KB Output is correct
9 Correct 1 ms 1776 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
11 Correct 63 ms 8032 KB Output is correct
12 Correct 16 ms 2112 KB Output is correct
13 Correct 57 ms 3292 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 13 ms 1956 KB Output is correct
18 Correct 51 ms 3284 KB Output is correct
19 Correct 43 ms 3288 KB Output is correct
20 Correct 12 ms 2108 KB Output is correct
21 Correct 51 ms 3316 KB Output is correct
22 Correct 54 ms 4356 KB Output is correct
23 Correct 62 ms 8072 KB Output is correct
24 Correct 32 ms 2712 KB Output is correct
25 Correct 51 ms 3244 KB Output is correct
26 Correct 4 ms 2140 KB Output is correct
27 Correct 3 ms 2144 KB Output is correct
28 Correct 1 ms 1636 KB Output is correct
29 Correct 3 ms 2144 KB Output is correct
30 Correct 4 ms 2152 KB Output is correct
31 Correct 2 ms 1892 KB Output is correct
32 Correct 4 ms 2152 KB Output is correct
33 Correct 3 ms 2112 KB Output is correct
34 Correct 1 ms 1640 KB Output is correct
35 Correct 1 ms 1640 KB Output is correct
36 Correct 1 ms 1640 KB Output is correct
37 Correct 68 ms 6384 KB Output is correct
38 Correct 58 ms 7728 KB Output is correct
39 Correct 47 ms 3284 KB Output is correct
40 Correct 1 ms 1628 KB Output is correct
41 Correct 2 ms 1884 KB Output is correct
42 Correct 71 ms 8288 KB Output is correct
43 Correct 77 ms 8140 KB Output is correct
44 Correct 56 ms 3284 KB Output is correct
45 Correct 25 ms 2140 KB Output is correct