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 int 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;
vector<int> ok;
struct sub1{
int par[maxn];
int root(int x)
{
return par[x] < 0 ? x : par[x] = root(par[x]);
}
bool unite(int u, int v)
{
if ((u = root(u)) == (v = root(v))) return 0;
if (par[u] > par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
return 1;
}
sub1()
{
sort(all(edges));
int res = 1e18;
for (int mask = 1; mask < 1 << n; mask++)
{
for (int i = 1; i <= n; i++)
par[i] = -1;
int ans = 0;
for (auto i : edges)
{
int w = i[0], u = i[1], v = i[2];
if ((mask >> (u - 1) & 1) && (mask >> (v - 1) & 1))
ans += w * unite(u, v);
}
int check = 0;
for (int j = 1; j < ok.size(); j++)
check |= unite(ok[j - 1], ok[j]);
if (!check)
res = min(res, ans);
}
cout << res;
}
};
struct full{
#define iii array<int, 3>
int d[maxn][32];
full()
{
priority_queue<iii, vector<iii>, greater<iii>> pq;
memset(d, 0x3f, sizeof d);
int 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[i][bit] = 0;
pq.push({0, i, bit});
}
int full = (1 << k) - 1;
while (pq.size())
{
auto to = pq.top();
pq.pop();
int val = to[0], u = to[1], bit = to[2];
if (val != d[u][bit]) continue;
for (auto i : adj[u])
{
int v, w;
tie(v, w) = i;
int not_mask = bit ^ full;
if(d[v][bit] > val + w)
pq.push({d[v][bit] = val + w, v, bit});
for(int s = not_mask; s > 0; s = (s - 1) & not_mask)
if(d[v][bit | s] > val + d[v][s] + w)
pq.push({d[v][bit | s] = val + d[v][s] + w, v, bit | s});
}
}
int res = oo;
for (int i = 1; i <= n; i++)
res = min(res, d[i][full]);
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});
if (n <= 20)
{
sub1 solve;
}
else
{
full solve;
}
}
Compilation message (stderr)
cities.cpp: In constructor 'sub1::sub1()':
cities.cpp:43:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int j = 1; j < ok.size(); j++)
| ~~^~~~~~~~~~~
# | 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... |