제출 #1348865

#제출 시각아이디문제언어결과실행 시간메모리
1348865iamhereforfunCities (BOI16_cities)C++20
100 / 100
1159 ms43612 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 1e5 + 5;
const int M = 1 << 5;
const int K = 19;
const int LG = 20;
const long long INF = 3e18;
const int C = 26;
const int B = 1000;
const int MOD = 1e9 + 7;

int n, k, m;
vector<pair<int, int>> adj[N];
vector<int> icity;
long long dp[M][N], ans;
bool vis[M][N];

inline void solve()
{
    cin >> n >> k >> m;
    for (int x = 0; x < M; x++)
    {
        for (int y = 1; y <= n; y++)
        {
            dp[x][y] = INF;
            vis[x][y] = 0;
        }
    }
    for (int x = 0; x < k; x++)
    {
        int i;
        cin >> i;
        icity.push_back(i);
        dp[(1 << x)][i] = 0;
    }
    for (int x = 0; x < m; x++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
    }
    for (int x = 1; x <= k; x++)
    {
        for (int y = 0; y < (1 << k); y++)
        {
            if (__builtin_popcount(y) != x)
                continue;
            priority_queue<pair<long long, int>> pq;
            for (int z = 1; z <= n; z++)
            {
                if (dp[y][z] != INF)
                {
                    pq.push({-dp[y][z], z});
                }
            }
            while (!pq.empty())
            {
                auto [a, c] = pq.top();
                pq.pop();
                if (vis[y][c])
                    continue;
                vis[y][c] = 1;
                for (auto &[i, j] : adj[c])
                {
                    if (dp[y][i] > dp[y][c] + j)
                    {
                        dp[y][i] = dp[y][c] + j;
                        pq.push({-dp[y][i], i});
                    }
                }
            }
        }
        for (int y = 0; y < (1 << k); y++)
        {
            if (__builtin_popcount(y) != x)
                continue;
            // for(int z = 1; z <= n; z++)
            // {
            //     cout << dp[y][z] << " " << bitset<3>(y) << " " << z << "\n";
            // }
            for (int z = 0; z < k; z++)
            {
                if (y >> z & 1)
                    continue;
                for (int t = 1; t <= n; t++)
                {
                    dp[y ^ (1 << z)][t] = min(dp[y ^ (1 << z)][t], dp[y][t] + dp[(1 << z)][t]);
                }
            }
        }
    }
    ans = INF;
    for (int x = 1; x <= n; x++)
    {
        ans = min(ans, dp[(1 << k) - 1][x]);
    }
    cout << ans << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...