P5651 基础最短路练习题
思路:
题目中给出定义一条简单路径的权值为路径上所有边边权的异或和,那么我们首先要得出一个结论 x x x x o r xor xor x x x = = = 0 0 0 也就是说,我们再求路径权值时假设有 a 1 a_1 a1 a 2 a_2 a2 a 3 a_3 a3 a 4 a_4 a4 a 5 a_5 a5 a 6 a_6 a6和边 w 1 w_1 w1 w 2 w_2 w2 w 3 w_3 w3 w 4 w_4 w4 w 5 w_5 w5
举个例子 a 1 a_1 a1到 a 4 a_4 a4点和到 a 6 a_6 a6距离就是
圈起来的就是异或…
所以
a
4
a _4
a4与
a
6
a_6
a6之间就有
可以看出,最后答案就是两者之间的异或,那么我们只需要用 b f s bfs bfs将每一个第一次扩展到的点就行异或,最后得出两点异或值即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<queue>
using namespace std;
typedef long long ll;
const int p = 1e9 + 7;
typedef pair<int, int> pii;
const int N = 4e5 + 10;
int e[N], ne[N], h[N], w[N], idx;
void add(int x, int y, int z) {
e[idx] = y, ne[idx] = h[x], w[idx] = z, h[x] = idx++;
}
int dis[N];
bool st[N];
void bfs(int x) {
queue<int>q;
q.push(x);
while (q.size())
{
int t = q.front();
q.pop();
for (int i = h[t]; i != -1; i = ne[i]) {
int j = e[i];
if (!st[j]) {
dis[j] = dis[t] xor w[i];
st[j] = true;
q.push(j);
}
}
}
}
int main() {
memset(h, -1, sizeof h);
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
add(y, x, z);
}
bfs(1);
while (q--)
{
int x, y;
cin >> x >> y;
printf("%d\n", dis[x] ^ dis[y]);
}
}
输出用了 p r i n t f printf printf不知道为啥 c o u t cout cout报错。。。