이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* cerberus97 - Hanit Banga */
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int N = 1e5 + 10, K = 5, S = (1 << K) + 10;
const ll inf = 1e15 + 42;
struct state_t {
int mask, u;
ll val;
bool operator<(const state_t &o) const {
return val > o.val;
}
};
int imp[K];
vector<pii> g[N];
vector<int> supermasks[S];
ll dp[S][N];
int main() {
fast_cin();
int n, k, m;
cin >> n >> k >> m;
for (int i = 0; i < k; ++i) {
cin >> imp[i];
}
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
int tot = (1 << k), all = tot - 1;
for (int mask = 1; mask < tot; ++mask) {
for (int u = 1; u <= n; ++u) {
dp[mask][u] = inf;
}
}
for (int mask = 0; mask < tot; ++mask) {
for (int smask = mask + 1; smask < tot; ++smask) {
if ((smask & mask) == mask) {
supermasks[mask].pb(smask);
}
}
}
priority_queue<state_t> q;
for (int i = 0; i < k; ++i) {
dp[1 << i][imp[i]] = 0;
q.push({1 << i, imp[i], 0});
}
while (!q.empty()) {
auto cur = q.top();
q.pop();
int mask = cur.mask, u = cur.u;
if (dp[mask][u] != cur.val) {
continue;
}
for (auto &e : g[u]) {
int v = e.first, w = e.second;
ll cand = dp[mask][u] + w;
if (cand < dp[mask][v]) {
dp[mask][v] = cand;
q.push({mask, v, cand});
}
}
for (auto &nmask : supermasks[mask]) {
int extra = nmask ^ mask;
ll cand = dp[mask][u] + dp[extra][u];
if (cand < dp[nmask][u]) {
dp[nmask][u] = cand;
q.push({nmask, u, cand});
}
}
}
ll ans = inf;
for (int u = 1; u <= n; ++u) {
ans = min(ans, dp[all][u]);
}
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... |