이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5, K = 5;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
int n, k, m; cin >> n >> k >> m;
static int v[K];
for (int i = 0; i < k; i++) cin >> v[i], v[i]--;
static vector<pair<int, int>> g[N];
while (m--) {
int i, j, w; cin >> i >> j >> w, i--, j--;
g[i].push_back({j, w}), g[j].push_back({i, w});
}
static long long d[K][N];
for (int s = 0; s < k; s++) {
long long *t = d[s];
priority_queue<pair<long long, int>> q;
memset(t, 0x3f, n * 8);
q.push({-(t[v[s]] = 0), v[s]});
while (!q.empty()) {
auto [x, i] = q.top(); x *= -1, q.pop();
if (t[i] != x) continue;
for (auto [j, w] : g[i])
if (x + w < t[j])
q.push({-(t[j] = x + w), j});
}
}
static long long f[N][1 << K];
for (int i = 0; i < n; i++) memset(f[i], 0x3f, (1 << k) * 8);
priority_queue<tuple<long long, int, int>> q;
for (int i = 0; i < n; i++) q.push({-(f[i][1 << i] = 0), v[i], 1 << i});
while (!q.empty()) {
auto [x, i, b] = q.top(); x *= -1; q.pop();
if (f[i][b] != x) continue;
for (auto [j, w] : g[i])
if (x + w < f[j][b])
q.push({-(f[j][b] = x + w), j, b});
for (int s = 0; s < k; s++)
if ((b >> s & 1) == 0 && x + d[s][i] < f[i][b | (1 << s)])
q.push({-(f[i][b | 1 << s] = x + d[s][i]), i, b | (1 << s)});
}
long long ans = LLONG_MAX;
for (int i = 0; i < n; i++) ans = min(ans, f[i][(1 << k) - 1]);
cout << ans << '\n';
}
# | 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... |