Submission #996803

#TimeUsernameProblemLanguageResultExecution timeMemory
996803underwaterkillerwhaleCities (BOI16_cities)C++17
100 / 100
1621 ms85948 KiB
#include <bits/stdc++.h>
#define se              second
#define fs              first
#define mp              make_pair
#define pb              push_back
#define ll              long long
#define ii              pair<ll,ll>
#define ld              long double
#define SZ(v)           (int)v.size()
#define ALL(v)          v.begin(), v.end()
#define bit(msk, i)     ((msk >> i) & 1)
#define iter(id, v)     for(auto id : v)
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }

const int N  = 2e5 + 7;
const int Mod = 998244353;
const int szBL = 916;
const ll INF = 1e18;
const int BASE = 137;

int n, K, m;
vector<pair<int,ll>> ke[N];
ll dp[N][(1 << 5) + 7]; ///suppose i is root

void solution () {
    cin >> n >> K >> m;
    memset(dp, 0x3f, sizeof dp);
    rep (i, 1, K) {
        int x;
        cin >> x;
        dp[x][(1 << i - 1)] = 0;
    }
    rep (i, 1, m) {
        ll u, v, c;
        cin >> u >> v >> c;
        ke[u].push_back({v, c});
        ke[v].push_back({u, c});
    }
    rep (msk, 1, (1 << K) - 1) {
        if (__builtin_popcount(msk) > 1) {
            rep (i, 1, n) {
                rep (j, 0, K - 1) if (bit (msk, j) ) {
                        int nmsk = msk ^ (1 << j);
                        dp[i][msk] = min(dp[i][msk], dp[i][nmsk] + dp[i][(1 << j)]);
                }
            }
        }
        static priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
        rep (i, 1, n) {
            pq.push({dp[i][msk], i});
        }
        while (!pq.empty()) {
            int u = pq.top().se;
            pq.pop();
            iter (&id, ke[u]) {
                int v = id.fs, w = id.se;
                if (dp[v][msk] > dp[u][msk] + w) {
                    dp[v][msk] = dp[u][msk] + w;
                    pq.push({dp[v][msk], v});
                }
            }
        }
//        rep (i, 1, n) cout << msk<<" "<<i<<" "<<dp[i][msk]<<"\n";
    }
    ll res = INF;
    rep (i, 1, n) {
        res = min(res, dp[i][(1 << K) - 1]);
    }
    cout << res <<"\n";


}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//    file ("fbuy");
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
7
1 2 4
2 3 12
3 4 2
4 5 48
5 6 2
6 7 123

*/

Compilation message (stderr)

cities.cpp: In function 'void solution()':
cities.cpp:37:23: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   37 |         dp[x][(1 << i - 1)] = 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...