답안 #943334

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943334 2024-03-11T11:00:24 Z GrandTiger1729 Cities (BOI16_cities) C++17
74 / 100
6000 ms 23200 KB
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1e18;
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n, K, m;
    cin >> n >> K >> m;
    vector<int> a(K);
    for (int i = 0; i < K; i++)
    {
        cin >> a[i];
        a[i]--;
    }
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0; i < m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }
    auto dijkstra = [&](int i)
    {
        priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
        pq.emplace(0, i);
        vector<long long> dis(n, INF);
        vector<bool> vis(n);
        while (pq.size())
        {
            auto [d, u] = pq.top();
            pq.pop();
            if (vis[u])
            {
                continue;
            }
            vis[u] = 1;
            dis[u] = d;
            for (auto &[v, w] : g[u])
            {
                if (dis[v] > dis[u] + w)
                {
                    dis[v] = dis[u] + w;
                    pq.emplace(dis[v], v);
                }
            }
        }
        return dis;
    };
    auto merge = [&](const vector<long long> &b, const vector<long long> &c) -> vector<long long>
    {
        priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
        for (int i = 0; i < n; i++)
        {
            pq.emplace(b[i] + c[i], i);
        }
        vector<long long> ret(n, INF);
        vector<bool> vis(n);
        while (pq.size())
        {
            auto [d, u] = pq.top();
            pq.pop();
            if (vis[u])
            {
                continue;
            }
            vis[u] = 1;
            ret[u] = d;
            for (auto &[v, w] : g[u])
            {
                if (ret[v] > ret[u] + w)
                {
                    ret[v] = ret[u] + w;
                    pq.emplace(ret[v], v);
                }
            }
        }
        return ret;
    };
    sort(a.begin(), a.end());
    long long ans = INF;
    do
    {
        vector<long long> res = dijkstra(a[0]);
        for (int i = 1; i < K - 1; i++)
        {
            res = merge(res, dijkstra(a[i]));
        }
        ans = min(ans, res[a[K - 1]]);
    }
    while (next_permutation(a.begin(), a.end() - 1));
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 238 ms 21132 KB Output is correct
2 Correct 237 ms 22412 KB Output is correct
3 Correct 119 ms 13328 KB Output is correct
4 Correct 43 ms 8632 KB Output is correct
5 Correct 89 ms 15056 KB Output is correct
6 Correct 38 ms 8532 KB Output is correct
7 Correct 2 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 604 KB Output is correct
2 Correct 6 ms 656 KB Output is correct
3 Correct 4 ms 600 KB Output is correct
4 Correct 4 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1033 ms 23200 KB Output is correct
2 Correct 1041 ms 21584 KB Output is correct
3 Correct 529 ms 13740 KB Output is correct
4 Correct 599 ms 16056 KB Output is correct
5 Correct 209 ms 11044 KB Output is correct
6 Correct 67 ms 10324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5670 ms 22332 KB Output is correct
2 Execution timed out 6019 ms 22524 KB Time limit exceeded
3 Halted 0 ms 0 KB -