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>
#define ll long long
#define all(x) x.begin(), x.end()
using namespace std;
const int maxn = 1e5 + 5;
int n, k, m;
int chosen[maxn];
vector<pair<int,int>> adj[maxn];
vector<array<int, 3>> edges;
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
} return false;
}
vector<int> ok;
#define iii tuple<ll,int,int>
ll d[32][maxn];
void full()
{
priority_queue<iii, vector<iii>, greater<iii>> pq;
memset(d, 0x3f, sizeof d);
ll oo = d[0][0];
for (int i = 1; i <= n; i++)
{
int bit = 0;
if (chosen[i])
bit = 1 << (chosen[i] - 1);
else continue;
d[bit][i] = 0;
pq.emplace(0, i, bit);
}
int full = (1 << k) - 1;
while (pq.size())
{
auto to = pq.top();
pq.pop();
ll val; int u, bit;
tie(val, u, bit) = to;
// ll val = to[0], u = to[1], bit = to[2];
if (val != d[bit][u]) continue;
for (auto i : adj[u])
{
ll v, w;
tie(v, w) = i;
if(minimize(d[bit][v], d[bit][u] + w)){
pq.push({d[bit][v], v, bit});
}
int not_mask = bit ^ full;
for(int s = not_mask; s > 0; s = (s - 1) & not_mask){
if(minimize(d[bit | s][v], d[bit][u] + d[s][v] + w)){
pq.push({d[bit | s][v], v, bit | s});
}
}
}
}
ll res = oo;
for (int i = 1; i <= n; i++)
res = min(res, d[full][i]);
cout << res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
cin >> n >> k >> m;
for (int i = 1, x; i <= k; i++)
cin >> x, chosen[x] = 1;
k = accumulate(chosen + 1, chosen + n + 1, 0ll);
for (int i = 1; i <= n; i++)
if (chosen[i])
ok.emplace_back(i),
chosen[i] = ok.size();
for (int i = 1, u, v, w; i <= m; i++)
cin >> u >> v >> w,
adj[u].emplace_back(v, w),
adj[v].emplace_back(u, w),
edges.push_back({w, u, v});
full();
}
# | 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... |