답안 #758888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758888 2023-06-15T12:22:47 Z ProtonDecay314 Voting Cities (NOI22_votingcity) C++17
100 / 100
350 ms 93796 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

struct node {
    ll i;
    ll S;
    ll ind() {
        return (i << 5ll) | S;
    }
};

node node_from_ind(ll ind) {
    node e = {ind >> 5ll, ind & 0b11111};
    return e;
}

int main() {
    ll n, e, k;
    cin >> n >> e >> k;
    const ll num_tickets = 5ll;
    const ll ticket_combs = 1ll << num_tickets;

    vector<vector<pll>> out_adj;
    vector<vector<pll>> in_adj;
    out_adj.reserve(n * ticket_combs);
    in_adj.reserve(n * ticket_combs);
    for(ll i = 0ll; i < n * ticket_combs; i++) {
        vector<pll> out_row;
        vector<pll> in_row;
        out_adj.push_back(out_row);
        in_adj.push_back(in_row);
    }

    vector<ll> v_cities;
    for(ll i = 0; i < k; i++) {
        ll curcity;
        cin >> curcity;
        curcity <<= num_tickets;
        for(ll j = 0ll; j < ticket_combs; j++) {
            v_cities.push_back(curcity | j);
        }
    }

    // Build the graph
    for(ll i = 0ll; i < e; i++) {
        ll u, v, c;
        cin >> u >> v >> c;

        for(ll tickets = 0ll; tickets < ticket_combs; tickets++) {
            out_adj[(u << num_tickets) | tickets].push_back({(v << num_tickets) | tickets, c});
            in_adj[(v << num_tickets) | tickets].push_back({(u << num_tickets) | tickets, c});

            for(ll bitpos = 0ll; bitpos < 5ll; bitpos++) {
                if((tickets >> bitpos) & 0b1) {
                    out_adj[(u << num_tickets) | tickets].push_back({(v << num_tickets) | (tickets ^ (1ll << bitpos)), (c / 10ll) * (10ll - bitpos - 1ll)});
                    in_adj[(v << num_tickets) | (tickets ^ (1ll << bitpos))].push_back({(u << num_tickets) | tickets, (c / 10ll) * (10ll - bitpos - 1ll)});
                }
            }
        }
    }

    // Dijkstra from the cities
    vector<ll> dist(n * ticket_combs, -1ll);
    priority_queue<pll, vector<pll>, greater<pll>> pq;
    
    for(ll v_city : v_cities) {
        pq.push({0ll, v_city});
    }

    while(!pq.empty()) {
        ll curd, curn;
        tie(curd, curn) = pq.top();
        pq.pop();

        if(dist[curn] != -1ll) continue;
        dist[curn] = curd;

        for(pll e : in_adj[curn]) {
            pq.push({curd + e.second, e.first});
        }
    }

    // for(ll i = 0ll; i < n * ticket_combs; i++) {
    //     std::cout << "DEBUG: dist[" << i << "] = " << dist[i] << endl;
    // }

    ll q;
    cin >> q;

    while(q--) {
        ll s;
        vector<ll> p(5ll, 0ll);
        cin >> s >> p[0] >> p[1] >> p[2] >> p[3] >> p[4];

        ll ans = numeric_limits<ll>::max();

        for(ll i = 0ll; i < ticket_combs; i++) {
            bool valid = true;
            ll total_ticket_price = 0ll;
            for(ll bitpos = 0ll; bitpos < num_tickets; bitpos++) {
                if(((i >> bitpos) & 1ll)) {
                    ll ticket_price = p[bitpos];
                    if(ticket_price == -1ll) {
                        valid = false;
                        break;
                    }
                    total_ticket_price += ticket_price;
                }
            }

            if(valid) {
                ans = min(ans, total_ticket_price + dist[(s << num_tickets) | i]);
            }
        }

        std::cout << ans << endl;
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 61544 KB Output is correct
2 Correct 109 ms 35844 KB Output is correct
3 Correct 289 ms 63740 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 61544 KB Output is correct
2 Correct 109 ms 35844 KB Output is correct
3 Correct 289 ms 63740 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 272 ms 63672 KB Output is correct
7 Correct 99 ms 35792 KB Output is correct
8 Correct 277 ms 63812 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 61544 KB Output is correct
2 Correct 109 ms 35844 KB Output is correct
3 Correct 289 ms 63740 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 272 ms 63672 KB Output is correct
7 Correct 99 ms 35792 KB Output is correct
8 Correct 277 ms 63812 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 304 ms 68240 KB Output is correct
12 Correct 108 ms 35960 KB Output is correct
13 Correct 272 ms 63780 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 61544 KB Output is correct
2 Correct 109 ms 35844 KB Output is correct
3 Correct 289 ms 63740 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 119 ms 59440 KB Output is correct
7 Correct 118 ms 35840 KB Output is correct
8 Correct 277 ms 63712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 61544 KB Output is correct
2 Correct 109 ms 35844 KB Output is correct
3 Correct 289 ms 63740 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 272 ms 63672 KB Output is correct
7 Correct 99 ms 35792 KB Output is correct
8 Correct 277 ms 63812 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 119 ms 59440 KB Output is correct
12 Correct 118 ms 35840 KB Output is correct
13 Correct 277 ms 63712 KB Output is correct
14 Correct 255 ms 63600 KB Output is correct
15 Correct 98 ms 35788 KB Output is correct
16 Correct 283 ms 63788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 277 ms 63872 KB Output is correct
2 Correct 291 ms 68224 KB Output is correct
3 Correct 130 ms 36524 KB Output is correct
4 Correct 304 ms 63856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 7880 KB Output is correct
2 Correct 22 ms 7924 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 17 ms 5856 KB Output is correct
5 Correct 20 ms 7368 KB Output is correct
6 Correct 4 ms 1108 KB Output is correct
7 Correct 27 ms 7880 KB Output is correct
8 Correct 22 ms 7884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 280 ms 61544 KB Output is correct
2 Correct 109 ms 35844 KB Output is correct
3 Correct 289 ms 63740 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 272 ms 63672 KB Output is correct
7 Correct 99 ms 35792 KB Output is correct
8 Correct 277 ms 63812 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 304 ms 68240 KB Output is correct
12 Correct 108 ms 35960 KB Output is correct
13 Correct 272 ms 63780 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 119 ms 59440 KB Output is correct
17 Correct 118 ms 35840 KB Output is correct
18 Correct 277 ms 63712 KB Output is correct
19 Correct 255 ms 63600 KB Output is correct
20 Correct 98 ms 35788 KB Output is correct
21 Correct 283 ms 63788 KB Output is correct
22 Correct 277 ms 63872 KB Output is correct
23 Correct 291 ms 68224 KB Output is correct
24 Correct 130 ms 36524 KB Output is correct
25 Correct 304 ms 63856 KB Output is correct
26 Correct 26 ms 7880 KB Output is correct
27 Correct 22 ms 7924 KB Output is correct
28 Correct 2 ms 980 KB Output is correct
29 Correct 17 ms 5856 KB Output is correct
30 Correct 20 ms 7368 KB Output is correct
31 Correct 4 ms 1108 KB Output is correct
32 Correct 27 ms 7880 KB Output is correct
33 Correct 22 ms 7884 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 300 KB Output is correct
36 Correct 1 ms 304 KB Output is correct
37 Correct 282 ms 67976 KB Output is correct
38 Correct 350 ms 93796 KB Output is correct
39 Correct 263 ms 63744 KB Output is correct
40 Correct 5 ms 9044 KB Output is correct
41 Correct 25 ms 14504 KB Output is correct
42 Correct 301 ms 68336 KB Output is correct
43 Correct 347 ms 93728 KB Output is correct
44 Correct 291 ms 63716 KB Output is correct
45 Correct 123 ms 36172 KB Output is correct