이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 1e5 + 5;
const long long inf = 1e18;
using ll = long long;
int idx[mxN];
vector<pair<int, int>> ad[mxN];
vector<int> terminals;
ll dist[5][mxN];
ll d[mxN][32];
int n, k;
void dijkstra(int s) {
int t = idx[s];
for (int i = 1; i <= n; i++) dist[t][i] = inf;
priority_queue<pair<ll, int>> pq;
pq.push({0, s});
dist[t][s] = 0;
while (!pq.empty()) {
auto [dd, u] = pq.top();
pq.pop();
if (dist[t][u] != -dd) continue;
for (auto [v, w] : ad[u]) {
if (dist[t][v] > dist[t][u] + w) {
dist[t][v] = dist[t][u] + w;
pq.push({-dist[t][v], v});
}
}
}
}
ll solve(int s) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (1 << k); j++) d[i][j] = inf;
}
priority_queue<tuple<ll, int, int>> pq;
pq.push({0, s, 1 << idx[s]});
d[s][1 << idx[s]] = 0;
while (!pq.empty()) {
auto [_, u, mask] = pq.top();
pq.pop();
if (d[u][mask] != -_) continue;
for (int j = 0; j < k; j++) {
if (mask >> j & 1) continue;
int new_mask = mask | (1 << j);
if (d[u][new_mask] > d[u][mask] + dist[j][u]) {
d[u][new_mask] = d[u][mask] + dist[j][u];
pq.push({-d[u][new_mask], u, new_mask});
}
}
for (auto [v, w] : ad[u]) {
if (d[v][mask] > d[u][mask] + w) {
d[v][mask] = d[u][mask] + w;
pq.push({-d[v][mask], v, mask});
}
}
}
ll ans = inf;
for (int i = 1; i <= n; i++) {
ans = min(ans, d[i][(1 << k) - 1]);
}
return ans;
}
void testCase() {
int m;
cin >> n >> k >> m;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
terminals.push_back(x);
idx[x] = i;
}
while (m--) {
int u, v, w;
cin >> u >> v >> w;
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
for (int t : terminals) {
dijkstra(t);
}
ll ans = inf;
for (int t : terminals) {
ans = min(ans, solve(t));
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
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... |