#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<ll,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
vector<pii> adj[mn];
ll dist[mn][1 << 5], gather[mn];
bool vis[mn][1 << 5];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k, m; cin >> n >> k >> m;
for (int i = 1; i <= n; i++)
for (int mask = 0; mask < (1 << k); mask++) dist[i][mask] = LLONG_MAX;
priority_queue<tpl> pq;
vector<int> fav(k);
for (int i = 0; i < k; i++) {
cin >> fav[i];
gather[fav[i]] |= (1 << i);
}
for (int i = 1; i <= n; i++) {
dist[i][gather[i]] = 0;
if (gather[i]) pq.emplace(0, i, gather[i]);
}
while (m--) {
int a, b, c; cin >> a >> b >> c;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
while (pq.size()) {
ll d; int u, mask; tie(d, u, mask) = pq.top(); pq.pop();
if (vis[u][mask]) continue;
vis[u][mask] = 1;
for (pii it : adj[u]) {
int v, w; tie(v, w) = it;
for (int supr = mask; supr < (1 << k); supr = (supr + 1) | mask) {
if (dist[v][supr ^ mask] == LLONG_MAX) continue;
if (dist[u][mask] + dist[v][supr ^ mask] + w < dist[v][supr]) {
dist[v][supr] = dist[u][mask] + dist[v][supr ^ mask] + w;
pq.emplace(-dist[v][supr], v, supr);
}
}
}
}
ll ans = LLONG_MAX;
for (int i : fav)
ans = min(ans, dist[i][(1 << k) - 1]);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4916 KB |
Output is correct |
3 |
Correct |
1 ms |
4688 KB |
Output is correct |
4 |
Correct |
1 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
580 ms |
60588 KB |
Output is correct |
2 |
Correct |
524 ms |
60196 KB |
Output is correct |
3 |
Correct |
203 ms |
40124 KB |
Output is correct |
4 |
Correct |
59 ms |
14016 KB |
Output is correct |
5 |
Correct |
230 ms |
48320 KB |
Output is correct |
6 |
Correct |
44 ms |
13300 KB |
Output is correct |
7 |
Correct |
4 ms |
5200 KB |
Output is correct |
8 |
Correct |
2 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5580 KB |
Output is correct |
2 |
Correct |
7 ms |
5580 KB |
Output is correct |
3 |
Correct |
4 ms |
5200 KB |
Output is correct |
4 |
Correct |
6 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1912 ms |
109600 KB |
Output is correct |
2 |
Correct |
1959 ms |
109712 KB |
Output is correct |
3 |
Correct |
974 ms |
55408 KB |
Output is correct |
4 |
Correct |
1207 ms |
63648 KB |
Output is correct |
5 |
Correct |
272 ms |
36268 KB |
Output is correct |
6 |
Correct |
109 ms |
17596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5444 ms |
175200 KB |
Output is correct |
2 |
Correct |
5547 ms |
175500 KB |
Output is correct |
3 |
Correct |
5324 ms |
175180 KB |
Output is correct |
4 |
Correct |
2327 ms |
104772 KB |
Output is correct |
5 |
Correct |
3216 ms |
162016 KB |
Output is correct |
6 |
Correct |
668 ms |
52640 KB |
Output is correct |
7 |
Correct |
230 ms |
25008 KB |
Output is correct |