跳到正文

题解

题解:P9377 [THUPC 2023 决赛] 百合

GI录像机约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年7月21日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 7 分钟
获赞
2
题目
P9377

版本

  1. v1当前

题解:

这是一个乱搞做法,但达到了你谷最优解第一页,最慢点650ms。

考虑 dijkstra,直接建闪现边的话需要 O(22k)O(2^{2k}) 条边,显然是不优的。在 dijkstra 过程中,如果一个点被 ai<=aja_i<=a_j 的 aia_i 尝试更新过,那它显然不需要再被 aja_j更新,这是因为先前算完的点 disdis 一定小于当前点的 disdis。

故而建立 kk 个 vector,第 ii 个 vector 里存已经被 aia_i 尝试更新过的点的集合。

然后 dijkstra 时,算完每个点的 disdis 后,对于每个 i≤ki\le k 向堆中添加权值为 dis+aidis+a_i 的点,用这些点去更新所有 aj>aia_j>a_i 的集合 jj 中的点,把集合 jj 中满足 __builtin_popcount⁡(u⨁pos)=i\operatorname{\_\_builtin\_popcount}(u\bigoplus pos)=i,的点扔进集合 ii。

但是 i=1i=1 的情况下,遍历一次 O(n)O(n) 的复杂度却至多只能将 kk 个点放到 aa 更小的集合,显然不够优秀。

所以对于 i≤2i\le 2 或 i≥k−2i\ge k-2 的情况,我们直接枚举它们的 C(i,k)C(i,k) 个后继状态更新即可。

然后就通过此题了

代码:

C++
#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;
}
原文
存档评论2 条抓取于