跳到正文

题解

题解:AT_abc410_d [ABC410D] XOR Shortest Walk

xiao7_Mr_10_约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

闲话

这题我考场上用了一个很猎奇的做法,因为我做过 CF845G。但是不知道就挂了,可能是没特判什么的。

正文

这个题属于是会的越多,会的越少。什么意思呢?哎你看这个异或最小值可以 dfs 一遍处理环和链的值然后用线性基维护查最小值。(我纯属有病)

实际上点数和值域都很小,都在 10310^3 范围内就是启发你做 bfs。暴力计 visi,jvis_{i,j} 表示到点 ii 异或值为 jj 是否可行,然后直接每次枚举出边扩展即可。时间复杂度 O(nV)O(nV),其中 VV 是值域。

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e3+5;
struct Point{
	int v,val;
};
int n,m;
vector <Point> e[N];
queue <Point> q;int u,v,w,vis[N][N<<1];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= m;i++){
		cin >> u >> v >> w;
		e[u].push_back((Point){v,w});
	}q.push((Point){1,0});vis[1][0]=1;
	while(!q.empty()){
		int head=q.front().v,state=q.front().val;q.pop();
		for(int i = 0;i < e[head].size();i++){
			int v=e[head][i].v,val=e[head][i].val;
			if(!vis[v][val^state]){
				vis[v][val^state]=1;
				q.push((Point){v,val^state});
			}
		}
	}
	for(int i = 0;i <= 1024;i++){
		if(vis[n][i]){
			cout << i;
			return 0;
		}
	}cout <<-1;
	return 0;
}
原文