CodeForces845G-Shortest PathProblem?
You are given an undirected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). You have to find the minimum length of path between vertex 1 and vertex n.
Note that graph can contain multiple edges and loops. It is guaranteed that the graph is connected.
Input
The first line contains two numbers n and m (1 ≤ n ≤ 100000, n - 1 ≤ m ≤ 100000) — the number of vertices and the number of edges, respectively.
Then m lines follow, each line containing three integer numbers x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108). These numbers denote an edge that connects vertices x and y and has weight w.
Output
Print one number — the minimum length of path between vertices 1 and n.
Examples
Input3 3 1 2 3 1 3 23 2 0Output
2Input
2 21 1 3 1 2 3Output
0
这是一个求从地点1到地点N经过路的异或和最短路的题;对于与一个环我们都可以使其异或和为零(一个环经过两次即异或和为0),一个环要么走完一遍,要么不走;我们可以任意找一条从1到N的路,然后异或每一个环,找最小值即可;
AC代码为:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
typedef long long LL;
int n,m,u,v,w,tot,temp,first[maxn],vis[maxn],a[maxn],b[maxn],dis[maxn];
struct Edge{
int to,w,net;
} edge[maxn<<1];
inline void Init()
{
memset(first,-1,sizeof first);
memset(vis,0,sizeof vis);
memset(b,0,sizeof b);
tot=1;temp=0;
}
inline void addedge(int u,int v,int w)
{
edge[tot].to=v;
edge[tot].w =w;
edge[tot].net=first[u];
first[u]=tot++;
}
inline void dfs(int id,int len)
{
vis[id]=1; dis[id]=len;
for(int i=first[id];~i;i=edge[i].net)
{
if(vis[edge[i].to]) a[++temp]=dis[edge[i].to]^edge[i].w^len;
else dfs(edge[i].to,len^edge[i].w);
}
}
inline void Guass()
{
for(int i=1;i<=temp;i++)
{
for(int j=31;j>=0;j--)
{
if((a[i] >> j) & 1)
{
if(!b[j])
{
b[j]=a[i];
break;
}
else a[i]^=b[j];
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
Init();
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
addedge(u,v,w);
addedge(v,u,w);
}
dfs(1,0);
int ans=dis
;
for(int i=31;i>=0;i--) ans=min(ans,ans^b[i]);
cout<<ans<<endl;
return 0;
}
- CodeForces845G-Shortest PathProblem?
- Codeforces 845G Shortest Path Problem?(异或最短路)
- 线性基【CF845G】Shortest Path Problem?
- Codeforces 845G Shortest Path Problem
- Educational Codeforces Round 27 G. Shortest Path Problem?(Guass异或线性基)
- Codefroces Educational Round 27 845G Shortest Path Problem?
- Single-source shortest path problem: SPFA vs. Dijkstra
- 算法导论problem 24-5 Gabow's algorithm for single source shortest path
- 算法学习和实践 ---- Shortest Path Problem
- Educational Codeforces Round 27-搜索&技巧&异或-G. Shortest Path Problem?
- Educational Codeforces Round 27 G. Shortest Path Problem?
- Codeforces 845 G Shortest Path Problem?
- shortest path problem
- HDOJ 4725 The Shortest Path in Nya Graph(Dijkstra + 优先队列)
- hduThe shortest path(双调欧几里得旅行商问题)
- zoj 2760 How Many Shortest Path【最大流】
- hdu 5373 The shortest problem
- HDU5373——同余模——The shortest problem
- A. Shortest path of the king
- hdu 5373 - The shortest problem 解题报告