P9377 [THUPC 2023 决赛] 百合 题解
很难直接建出原图,“从
- 对给定的每条边
(x,y,c) ,建边(x,0,0)\stackrel{c}{\longleftrightarrow}(y,0,0) ; - 对状态
(x,i,j) (i<k ),建边(x,i,j)\stackrel{0}{\longrightarrow}(x,i+1,j) ; - 对状态
(x,i,j) (i<k ),建边(x,i,j)\stackrel{0}{\longrightarrow}(x\oplus2^i,i+1,j+1) ,其中\oplus 是按位异或; - 对状态
(x,k,j) ,建边(x,k,j)\stackrel{a_j}{\longrightarrow}(x,0,0) 。
这张图的点数是
注意到这张图里其实有很多
于是我们可以把这个过程单独提出来:松弛一个状态
于是只有在松弛非
最后就是这个东西空间有点紧,但是事实上我们只要维护形如
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using LL = long long;
using Pli = pair<LL, int>;
const int kK = 18, kN = (1 << kK - 1);
struct S {
int x, i, j;
} q[kN * 2];
int k, n, m, s, qh, qt, a[kK];
LL d[kN];
bool v[kN][kK][kK];
vector<pair<int, int>> e[kN];
priority_queue<Pli, vector<Pli>, greater<Pli>> pq;
void R(int x, LL _d) {
if (_d < d[x]) {
d[x] = _d, pq.emplace(_d, x);
}
}
void R(int x, int i, int j) {
if (!v[x][i][j]) {
v[x][i][j] = 1, q[++qt] = {x, i, j};
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> k >> m >> s, n = (1 << k);
for (int i = 1; i <= k; ++i) {
cin >> a[i];
}
for (int i = 1, x, y, w; i <= m; ++i) {
cin >> x >> y >> w;
e[x].emplace_back(y, w);
e[y].emplace_back(x, w);
}
fill_n(d, n, 1e18);
for (R(s, 0); !pq.empty();) {
int x = pq.top().second;
pq.pop();
for (auto i : e[x]) {
R(i.first, d[x] + i.second);
}
qh = 1, qt = 0;
for (R(x, 0, 0); qh <= qt; ++qh) {
S y = q[qh];
if (y.i < k) {
R(y.x, y.i + 1, y.j);
R(y.x ^ (1 << y.i), y.i + 1, y.j + 1);
} else {
R(y.x, d[x] + a[y.j]);
}
}
}
for (int i = 0; i < n; ++i) {
cout << d[i] << ' ';
}
return 0;
}