#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define f first
#define s second
#define inf 100000000000010
#define ll long long
#define li pair<ll, int>
int main() {
int n, k, m; cin >> n >> k >> m;
vector<int> A(k); for (int &x : A) { cin >> x; --x; }
vector<vector<ii>> adj(n);
for (int i = 0; i < m; i++) {
int a, b, w; cin >> a >> b >> w; --a; --b;
adj[a].push_back(make_pair(b, w));
adj[b].push_back(make_pair(a, w));
}
ll cost[k][n];
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) cost[i][j] = inf;
cost[i][A[i]] = 0;
priority_queue<li, vector<li>, greater<li>> pq; pq.push(make_pair(0, A[i]));
while (!pq.empty()) {
li u = pq.top(); pq.pop();
if (cost[i][u.s] < u.f) continue;
for (ii v : adj[u.s]) {
if (cost[i][v.f] > u.f + v.s) {
cost[i][v.f] = u.f + v.s;
pq.push(make_pair(u.f+v.s, v.f));
}
}
}
}
ll dist[n][(1 << k)];
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << k); j++) {
dist[i][j] = inf;
}
}
priority_queue<pair<ll, ii>, vector<pair<ll, ii>>, greater<pair<ll, ii>>> pq;
for (int i = 0; i < k; i++) {
dist[A[i]][(1 << i)] = 0;
pq.push(make_pair(0, make_pair(A[i], (1 << i))));
}
while (!pq.empty()) {
pair<ll, ii> top = pq.top(); pq.pop();
ii u = top.s;
if (dist[u.f][u.s] < top.f) continue;
for (ii v : adj[u.f]) {
ii nxt = make_pair(v.f, u.s);
ll d = dist[u.f][u.s] + v.s;
if (dist[nxt.f][nxt.s] > d) {
dist[nxt.f][nxt.s] = d;
pq.push(make_pair(d, nxt));
}
}
for (int i = 0; i < k; i++) {
ii nxt = make_pair(u.f, u.s|(1 << i));
ll d = dist[u.f][u.s] + cost[i][u.f];
if (dist[nxt.f][nxt.s] > d) {
dist[nxt.f][nxt.s] = d;
pq.push(make_pair(d, nxt));
}
}
}
ll best = inf;
int msk = (1 << k) - 1;
for (int i = 0; i < n; i++) {
best = min(best, dist[i][msk]);
}
cout << best << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1394 ms |
25908 KB |
Output is correct |
2 |
Correct |
1468 ms |
29600 KB |
Output is correct |
3 |
Correct |
777 ms |
20716 KB |
Output is correct |
4 |
Correct |
361 ms |
8852 KB |
Output is correct |
5 |
Correct |
812 ms |
22048 KB |
Output is correct |
6 |
Correct |
346 ms |
8676 KB |
Output is correct |
7 |
Correct |
7 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
896 KB |
Output is correct |
2 |
Correct |
10 ms |
1024 KB |
Output is correct |
3 |
Correct |
7 ms |
896 KB |
Output is correct |
4 |
Correct |
8 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2819 ms |
57760 KB |
Output is correct |
2 |
Correct |
2503 ms |
61548 KB |
Output is correct |
3 |
Correct |
1817 ms |
40284 KB |
Output is correct |
4 |
Correct |
1668 ms |
35916 KB |
Output is correct |
5 |
Correct |
622 ms |
18980 KB |
Output is correct |
6 |
Correct |
371 ms |
10644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5959 ms |
103848 KB |
Output is correct |
2 |
Correct |
5943 ms |
107996 KB |
Output is correct |
3 |
Correct |
5524 ms |
107572 KB |
Output is correct |
4 |
Correct |
3888 ms |
102860 KB |
Output is correct |
5 |
Correct |
3353 ms |
59092 KB |
Output is correct |
6 |
Correct |
911 ms |
20400 KB |
Output is correct |
7 |
Correct |
392 ms |
11248 KB |
Output is correct |