이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define f first
#define s second
#define inf 1000000010
typedef int realint;
#define int long long
realint 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));
}
int 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<ii, vector<ii>, greater<ii>> pq; pq.push(make_pair(0, A[i]));
while (!pq.empty()) {
ii u = pq.top(); pq.pop();
if (cost[i][u.s] < u.s) continue;
for (ii v : adj[u.s]) {
if (cost[i][v.f] > u.f) {
cost[i][v.f] = u.f + v.s;
pq.push(make_pair(u.f+v.s, v.f));
}
}
}
}
int 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<int, ii>, vector<pair<int, ii>>, greater<pair<int, 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<int, 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);
int 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));
int 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));
}
}
}
int best = inf;
int msk = (1 << (k+1)) - 1;
for (int i = 0; i < n; i++) {
best = min(best, dist[i][msk]);
}
cout << best << endl;
return 0;
}
# | 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... |