Submission #447004

#TimeUsernameProblemLanguageResultExecution timeMemory
447004prvocisloCities (BOI16_cities)C++17
100 / 100
2642 ms43344 KiB
#include <bits/stdc++.h> typedef long long ll; using namespace std; const ll inf = 1e18 + 5; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k, m; cin >> n >> k >> m; vector<int> im(k); for (int i = 0; i < k; i++) cin >> im[i], im[i]--; vector<vector<pair<int, int> > > g(n); for (int i = 0, a, b, c; i < m; i++) cin >> a >> b >> c, g[--a].push_back({--b, c}), g[b].push_back({a, c}); vector<vector<ll> > dp((1 << k), vector<ll>(n, inf)); dp[0].assign(n, 0); priority_queue<pair<ll, int> > pq; for (int m = 1; m < (1 << k); m++) { for (int i = 0; i < n; i++) { int id = find(im.begin(), im.end(), i) - im.begin(); if (m & (1 << id)) dp[m][i] = dp[m^(1 << id)][i]; else for (int s = m; s; s = (s-1)&m) dp[m][i] = min(dp[m][i], dp[s][i] + dp[m^s][i]); pq.push({-dp[m][i], i}); } vector<bool> vis(n, false); while (!pq.empty()) { ll d = -pq.top().first; int u = pq.top().second; pq.pop(); if (vis[u]) continue; vis[u] = true; for (const pair<int, int> &i : g[u]) { if (!vis[i.first] && dp[m][i.first] > d + i.second) { dp[m][i.first] = d + i.second; pq.push({-dp[m][i.first], i.first}); } } } } cout << (*min_element(dp[(1<<k)-1].begin(), dp[(1<<k)-1].end())) << "\n"; 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...