This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
for (int m = 1; m < (1 << k); m++)
{
set<pair<ll, int> > pq;
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.insert({dp[m][i], i});
}
vector<bool> vis(n, false);
while (!pq.empty())
{
ll d = pq.begin()->first; int u = pq.begin()->second;
pq.erase(pq.begin());
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)
{
pq.erase({dp[m][i.first], i.first});
dp[m][i.first] = d + i.second;
pq.insert({dp[m][i.first], i.first});
}
}
}
}
cout << (*min_element(dp[(1<<k)-1].begin(), dp[(1<<k)-1].end())) << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |