답안 #1075405

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1075405 2024-08-26T05:38:23 Z vjudge1 Cities (BOI16_cities) C++17
100 / 100
1427 ms 43496 KB
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
long long int f[33][100005], a[6];
vector<pair<int, long long int>> g[100005];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, k, m;
    cin >> n >> k >> m;
    for (int i = 0; i < k; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= m; i++) {
        int u, v, x;
        cin >> u >> v >> x;
        g[u].push_back({v, x});
        g[v].push_back({u, x});
    }
    for (int i = 1; i < (1 << k); i++) {
        int h = __builtin_popcount(i);
        priority_queue<pair<long long int, int>> q;
        if (h == 1) {
            for (int j = 1; j <= n; j++) f[i][j] = 1e18;
            for (int j = 0; j < k; j++) {
                if (i & (1 << j)) {
                    f[i][a[j]] = 0;
                    q.push({-f[i][a[j]], a[j]});
                }
            }
        }
        else {
            for (int j = 1; j <= n; j++) {
                f[i][j] = 1e18;
            }
            for (int j = 1; j <= n; j++) {
                for (int k = i; k > 0; k = i & (k - 1)) {
                    f[i][j] = min(f[i][j], f[k][j] + f[(i ^ k)][j]);
                }
                q.push({-f[i][j], j});
            }
        }
        while (!q.empty()) {
            pair<long long int, int> p = q.top();
            q.pop();
            long long int x = -p.first, u = p.second;
            if (f[i][u] != x) continue;
            for (auto w : g[u]) {
                if (f[i][w.first] > f[i][u] + w.second) {
                    f[i][w.first] = x + w.second;
                    q.push({-f[i][w.first], w.first});
                }
            }
        }
    }
    long long int res = 1e18;
    for (int i = 1; i <= n; i++) {
        res = min(res, f[(1 << k) - 1][i]);
    }
    cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2892 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 329 ms 24852 KB Output is correct
2 Correct 304 ms 24256 KB Output is correct
3 Correct 172 ms 16976 KB Output is correct
4 Correct 44 ms 11856 KB Output is correct
5 Correct 161 ms 19592 KB Output is correct
6 Correct 42 ms 11780 KB Output is correct
7 Correct 3 ms 2904 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3164 KB Output is correct
2 Correct 5 ms 3180 KB Output is correct
3 Correct 3 ms 2908 KB Output is correct
4 Correct 3 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 676 ms 31120 KB Output is correct
2 Correct 684 ms 30692 KB Output is correct
3 Correct 414 ms 23256 KB Output is correct
4 Correct 388 ms 22436 KB Output is correct
5 Correct 129 ms 13672 KB Output is correct
6 Correct 55 ms 13900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1427 ms 43472 KB Output is correct
2 Correct 1395 ms 43496 KB Output is correct
3 Correct 1371 ms 43148 KB Output is correct
4 Correct 896 ms 35724 KB Output is correct
5 Correct 722 ms 28860 KB Output is correct
6 Correct 177 ms 15116 KB Output is correct
7 Correct 58 ms 14420 KB Output is correct