#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 = 1; 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];
dist[fav[i]][1 << i] = 0;
pq.emplace(0, fav[i], 1 << 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 |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
1 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4916 KB |
Output is correct |
5 |
Correct |
2 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
666 ms |
56492 KB |
Output is correct |
2 |
Correct |
529 ms |
56100 KB |
Output is correct |
3 |
Correct |
217 ms |
41008 KB |
Output is correct |
4 |
Correct |
57 ms |
10440 KB |
Output is correct |
5 |
Correct |
207 ms |
44224 KB |
Output is correct |
6 |
Correct |
43 ms |
9932 KB |
Output is correct |
7 |
Correct |
4 ms |
5200 KB |
Output is correct |
8 |
Correct |
3 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5580 KB |
Output is correct |
2 |
Correct |
8 ms |
5580 KB |
Output is correct |
3 |
Correct |
5 ms |
5092 KB |
Output is correct |
4 |
Correct |
7 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1910 ms |
105684 KB |
Output is correct |
2 |
Correct |
1603 ms |
105284 KB |
Output is correct |
3 |
Correct |
841 ms |
53464 KB |
Output is correct |
4 |
Correct |
1145 ms |
59548 KB |
Output is correct |
5 |
Correct |
244 ms |
32432 KB |
Output is correct |
6 |
Correct |
98 ms |
14276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5509 ms |
171416 KB |
Output is correct |
2 |
Correct |
5513 ms |
171412 KB |
Output is correct |
3 |
Correct |
5071 ms |
171132 KB |
Output is correct |
4 |
Correct |
2205 ms |
102512 KB |
Output is correct |
5 |
Correct |
3191 ms |
157840 KB |
Output is correct |
6 |
Correct |
617 ms |
48804 KB |
Output is correct |
7 |
Correct |
227 ms |
21428 KB |
Output is correct |