题解:
这是一个乱搞做法,但达到了你谷最优解第一页,最慢点650ms。
考虑 dijkstra,直接建闪现边的话需要 条边,显然是不优的。在 dijkstra 过程中,如果一个点被 的 尝试更新过,那它显然不需要再被 更新,这是因为先前算完的点 一定小于当前点的 。
故而建立 个 vector,第 个 vector 里存已经被 尝试更新过的点的集合。
然后 dijkstra 时,算完每个点的 后,对于每个 向堆中添加权值为 的点,用这些点去更新所有 的集合 中的点,把集合 中满足 ,的点扔进集合 。
但是 的情况下,遍历一次 的复杂度却至多只能将 个点放到 更小的集合,显然不够优秀。
所以对于 或 的情况,我们直接枚举它们的 个后继状态更新即可。
然后就通过此题了
代码:
#include<bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char c = getchar();
while(c > '9' || c < '0') {
if(c == '-')f = -f;
c = getchar();
}
while(c <= '9' && c >= '0') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
void write(int x) {
if(x < 0) {
putchar('-');
x = -x;
} if(x > 9)write(x / 10);
putchar((x % 10) + '0');
}
const int N = (1 << 17) + 10, INF = 1 << 30, M = 2e5 + 10, PP = 1e6;
int k, m, s, a[20], head[N], tot, n, dis[N], rem[N];
bool vis[N];
vector<int>vt[20], tmp;
priority_queue<pair<int, int> >q;
struct Edge {
int to, nxt, w;
}e[M << 1];
void add(int u, int v, int w) {
e[++tot].to = v;
e[tot].w = w;
e[tot].nxt = head[u];
head[u] = tot;
}
int calc(int i, int j) {
return __builtin_popcount(i ^ j);
}
bool update(int fr, int to, int w) {
if(dis[to] > dis[fr] + w) {
dis[to] = dis[fr] + w;
q.push({-dis[to], to});
return 1;
} return 0;
}
void dijkstra() {
while(!q.empty()) {
int pos = q.top().second;
q.pop();
if(pos <= n) {
if(vis[pos])continue;
vis[pos] = 1;
for(int i = head[pos]; i; i = e[i].nxt) {
if(vis[e[i].to])continue;
update(pos, e[i].to, e[i].w);
}
for(int i = 1; i <= k; i++)q.push({-dis[pos] - a[i], pos + i * PP});
} else {
pos = pos;
int id = pos / PP, to;
pos %= PP;
if(id <= 2 || id >= k - 2) {
if(id == k) {
to = pos ^ (n - 1);
if(update(pos, to, a[id])) {
rem[to] = id;
vt[id].push_back(to);
}
} else if(id == k - 1 || id == 1) {
if(id == k - 1)to = pos ^ (n - 1);
else to = pos;
for(int i = 1; i <= k; i++) {
to ^= (1 << (i - 1));
if(update(pos, to, a[id])) {
rem[to] = id;
vt[id].push_back(to);
}
to ^= (1 << (i - 1));
}
} else if(id == k - 2 || id == 2){
if(id == k - 2)to = pos ^ (n - 1);
else to = pos;
for(int i = 1; i <= k; i++) {
to ^= (1 << (i - 1));
for(int j = i + 1; j <= k; j++) {
to ^= (1 << (j - 1));
if(update(pos, to, a[id])) {
rem[to] = id;
vt[id].push_back(to);
}
to ^= (1 << (j - 1));
}
to ^= (1 << (i - 1));
}
}
} else {
for(int i = 1; i <= k; i++) {
if(a[id] >= a[i])continue;
for(int j = 0; j < vt[i].size(); j++) {
int to = vt[i][j];
if(rem[to] != i)continue;
if(calc(pos, to) == id) {
update(pos, to, a[id]);
vt[id].push_back(to);
} else tmp.push_back(to);
}
swap(tmp, vt[i]);
tmp.clear();
}
}
}
}
}
signed main() {
//freopen("path.in", "r", stdin);
//freopen("path.out", "w", stdout);
k = read(), m = read(), s = read(), n = (1 << k);
for(int i = 1; i <= k; i++)a[i] = read();
for(int i = 0; i < n; i++)vt[calc(i, s)].push_back(i), dis[i] = a[calc(i, s)], rem[i] = calc(i, s), q.push({-dis[i], i});
for(int i = 1; i <= m; i++) {
int u = read(), v = read(), w = read();
add(u, v, w), add(v, u, w);
}
dijkstra();
for(int i = 0; i < n; i++) {
write(dis[i]);
putchar(' ');
}
return 0;
}