답안 #682980

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
682980 2023-01-17T11:58:13 Z bebra Cities (BOI16_cities) C++17
0 / 100
2892 ms 16304 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);
            vector<int> p(n, -1);

            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;
                    p[u] = v;
                    dist[u] = curr_dist + w;
                    pq.emplace(dist[u], u);
                }
            }

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

            if (curr_cost < min_cost[mask]) {
                min_cost[mask] = curr_cost;

                vector<int> subgraph = induced_subgraph[mask ^ (1 << i)];
                int j = vertices[i];
                while (p[j] != -1) {
                    subgraph.push_back(j);
                    j = p[j];
                }
                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 458 ms 16188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1067 ms 16260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2892 ms 16304 KB Output isn't correct
2 Halted 0 ms 0 KB -