답안 #682986

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
682986 2023-01-17T12:11:36 Z bebra Cities (BOI16_cities) C++17
0 / 100
5774 ms 14476 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define dbg(x) cerr << #x << ": " << x << endl;


const int MAX_N = 1e5 + 5;
const int MAX_M = 2e5 + 5;
const int MAX_K = 5;
const ll INF = 1e18;
vector<pair<int, int>> g[MAX_N];
int vertices[MAX_K];
vector<int> induced_subgraph[1 << MAX_K];
ll min_cost[1 << MAX_K];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k, m;
    cin >> n >> k >> m;
    for (int i = 0; i < k; ++i) {
        cin >> vertices[i];
        --vertices[i];
    }
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        int w;
        cin >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }
    for (int mask = 1; mask < (1 << k); ++mask) {
        int cnt = __builtin_popcount(mask);
        if (cnt == 1) {
            induced_subgraph[mask].push_back(vertices[__lg(mask)]);
            continue;
        }

        min_cost[mask] = INF;
        for (int i = 0; i < k; ++i) {
            if (!(mask & (1 << i))) continue;

            priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
            vector<bool> used(n);
            vector<ll> dist(n, INF);

            for (int j : induced_subgraph[mask ^ (1 << i)]) {
                pq.emplace(0, j);
                dist[j] = 0;
            }

            while (!pq.empty()) {
                auto [curr_dist, v] = pq.top();
                pq.pop();
                if (used[v]) continue;
                used[v] = true;
                for (const auto& [u, w] : g[v]) {
                    if (used[u]) continue;
                    if (curr_dist + w >= dist[u]) continue;
                    dist[u] = curr_dist + w;
                    pq.emplace(dist[u], u);
                }
            }


            vector<bool> used2(n);
            vector<ll> dist2(n, INF);
            priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq2;
            pq2.emplace(0, vertices[i]);
            dist2[vertices[i]] = 0;
            while (!pq2.empty()) {
                auto [curr_dist, v] = pq2.top();
                pq2.pop();
                if (used2[v]) continue;
                used2[v] = true;
                for (const auto& [u, w] : g[v]) {
                    if (used2[u]) continue;
                    if (curr_dist + w >= dist2[u]) continue;
                    dist2[u] = curr_dist + w;
                    pq2.emplace(dist2[u], u);
                }

            }

            ll curr_cost = min_cost[mask ^ (1 << i)] + dist[vertices[i]];
            if (curr_cost < min_cost[mask]) {
                min_cost[mask] = curr_cost;

                // creating subgraph
                vector<int> subgraph = induced_subgraph[mask ^ (1 << i)];
                for (int v = 0; v < n; ++v) {
                    if (dist[v] + dist2[v] == dist[vertices[i]]) {
                        subgraph.push_back(v);
                    }
                }

                sort(subgraph.begin(), subgraph.end());
                subgraph.resize(unique(subgraph.begin(), subgraph.end()) - subgraph.begin());

                induced_subgraph[mask] = subgraph;
            }
        }

    }
    cout << min_cost[(1 << k) - 1] << '\n';
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Incorrect 2 ms 2644 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 821 ms 14252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2324 ms 14432 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5774 ms 14476 KB Output isn't correct
2 Halted 0 ms 0 KB -