답안 #547294

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547294 2022-04-10T09:38:41 Z Jomnoi Cities (BOI16_cities) C++17
100 / 100
1784 ms 44784 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int MAX_N = 1e5 + 10;
const long long INF = 1e18 + 7;

int n;
vector <pair <int, int>> graph[MAX_N];
long long dist[32][MAX_N];

void Dijkstra(int mask) {
    priority_queue <pair <long long, int>> pq;
    for(int i = 1; i <= n; i++) {
        if(dist[mask][i] != INF) {
            pq.emplace(-dist[mask][i], i);
        }
    }
    while(!pq.empty()) {
        auto [d, u] = pq.top();
        pq.pop();

        if(dist[mask][u] < -d) {
            continue;
        }
        for(auto [v, w] : graph[u]) {
            if(dist[mask][u] + w < dist[mask][v]) {
                dist[mask][v] = dist[mask][u] + w;
                pq.emplace(-dist[mask][v], v);
            }
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int k, m;
    cin >> n >> k >> m;
    for(int i = 0; i < (1<<k); i++) {
        fill(dist[i], dist[i] + n + 1, INF);
    }
    for(int i = 0; i < k; i++) {
        int x;
        cin >> x;
        dist[1<<i][x] = 0;
    }
    while(m--) {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a].emplace_back(b, c);
        graph[b].emplace_back(a, c);
    }

    for(int mask = 1; mask < (1<<k); mask++) {
        if(__builtin_popcount(mask) > 1) {
            for(int s = mask; s > 0; s = (s - 1) & mask) {
                for(int i = 1; i <= n; i++) {
                    dist[mask][i] = min(dist[mask][i], dist[s][i] + dist[mask ^ s][i]);
                }
            }
        }
        Dijkstra(mask);
    }

    long long ans = INF;
    for(int i = 1; i <= n; i++) {
        ans = min(ans, dist[(1<<k) - 1][i]);
    }
    cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 2 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 457 ms 25780 KB Output is correct
2 Correct 429 ms 25764 KB Output is correct
3 Correct 226 ms 17796 KB Output is correct
4 Correct 57 ms 10864 KB Output is correct
5 Correct 222 ms 20472 KB Output is correct
6 Correct 56 ms 10832 KB Output is correct
7 Correct 4 ms 2812 KB Output is correct
8 Correct 3 ms 2788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2948 KB Output is correct
2 Correct 6 ms 3028 KB Output is correct
3 Correct 4 ms 2900 KB Output is correct
4 Correct 4 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 927 ms 32164 KB Output is correct
2 Correct 837 ms 32164 KB Output is correct
3 Correct 539 ms 24256 KB Output is correct
4 Correct 542 ms 22484 KB Output is correct
5 Correct 151 ms 13516 KB Output is correct
6 Correct 69 ms 12596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1777 ms 44784 KB Output is correct
2 Correct 1784 ms 44588 KB Output is correct
3 Correct 1782 ms 44684 KB Output is correct
4 Correct 1130 ms 36700 KB Output is correct
5 Correct 934 ms 28700 KB Output is correct
6 Correct 210 ms 14840 KB Output is correct
7 Correct 73 ms 12716 KB Output is correct