Shortest Path Problem?

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
Input
3 3
1 2 3
1 3 2
3 2 0
Output
2
Input
2 2
1 1 3
1 2 3
Output
0
最短路,取异或值最小,并且每一个点可以重复经过,dfs搜索,没经过的点扩展路径,经过的点更新异或值。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>//自定义最小函数取不了过去,会有影响,所以用了库函数
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
vector<pair<int,int> >v[];
vector<int>uxor;
int dis[],vis[];
int n,m,x,y,w;
void updatexor(int u)
{
for(int i=;i<uxor.size();i++)
{
u=min(u,u^uxor[i]);
}
if(u) uxor.push_back(u);
}
void dfs(int u,int result)
{
vis[u]=;
dis[u]=result;
for(int i=;i<v[u].size();i++)
{
if(!vis[v[u][i].first]) dfs(v[u][i].first,dis[u]^v[u][i].second);
else updatexor(dis[u]^v[u][i].second^dis[v[u][i].first]);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
scanf("%d%d%d",&x,&y,&w);
v[x].push_back(make_pair(y,w));
v[y].push_back(make_pair(x,w));
}
memset(vis,,sizeof(vis));
dfs(,);
for(int i=;i<uxor.size();i++)
{
dis[n]=min(dis[n],dis[n]^uxor[i]);
}
printf("%d\n",dis[n]);
return ;
}

Codefroces Educational Round 27 845G Shortest Path Problem?的更多相关文章

  1. 【CF edu 27 G. Shortest Path Problem?】

    time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...

  2. Codeforces 845G Shortest Path Problem?

    http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...

  3. Codefroces Educational Round 27 (A,B,C,D)

    A. Chess Tourney time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  5. Codeforces Beta Round #3 A. Shortest path of the king 水题

    A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...

  6. Codefroces Educational Round 26 837 D. Round Subset

    D. Round Subset time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Beta Round #3 A. Shortest path of the king

    标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include < ...

  8. 线性基【CF845G】Shortest Path Problem?

    Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...

  9. [CF845G]Shortest Path Problem?

    题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...

随机推荐

  1. ES6中includes、startsWith、endsWith

    es6新增includes:返回布尔值,表示是否找到字符串.startsWith:返回布尔值,表示字符串是否在源字符串的头部位置.endsWith:返回布尔值,表示参数字符串是否在源字符串尾部. va ...

  2. Bayes++ Library入门学习之熟悉class-Bayesian_filter_base(2)

    前面我们已经熟悉了Bayesian_filter::Bayes_filter_base和其子类的击继承关系,接下来我们开始学习该类的实现. bayesFlt.hpp文件为其实现主体,首先是两个常规的头 ...

  3. 使用freerdp远程连接Windows桌面(转载)

    使用freerdp远程连接Windows桌面 之前使用的是rdesktop,但是由于其不支持NLA认证,便不能登录公司的电脑.为此,现在使用freerdp——这是package的名字,实际的可执行程序 ...

  4. ArchLinux 音乐播放客户端ncmpcpp和服务端mpd的配置

    Ncmcpp是一个mpd客户端,它提供了很多方便的操作 MPD是一个服务器-客户端架构的音频播放器.功能包括音频播放, 播放列表管理和音乐库维护,所有功能占用的资源都很少. --取自 wiki.arc ...

  5. 使用let's encrypt为你的Ubuntu14+nginx网站保驾护航!

    finch最近正在研究一个新的网站系统,闲的没事想搞搞ssl,结果搞了两天,遇到很多问题,现在记录并分享一下经验. 环境之前搭建好了是Ubuntu14+nginx+php5+mysql 现在开始使用l ...

  6. 坑爹的RockSaw和坑爹的windows7

    坑爹的RockSaw和坑爹的windows7 http://chen4w.iteye.com/blog/1153433

  7. ArcGIS api for javascript——查找任务-没有地图查找要素

    描述 本例展示了如何使用查找任务搜索数据里的记录,然后在HTML表格中显示结果. 尽管FindTask不需要显示一个地图,但是构造函数需要一个ArcGIS Server地图服务的URL. 本例使用ES ...

  8. 函数指针的转换 & C的注意点

    再让我们看看<signal.h>中声明的signal函数void (*signal(int, void(*)(int)))(int)首先,用typedef简化,typedef void ( ...

  9. 为什么Java里的Arrays.asList不能用add和remove方法?

    在平时的开发过程中,我们知道能够将一个Array的对象转化为List.这种操作,我们仅仅要採用Arrays.asList这种方法即可了.笔者前段时间一直用这种方法,有一天,我发现通过Arrays.as ...

  10. java 究竟老年代和年轻代的比例为多大合适呢?

    眼下我还没有这方面过多的经验,和切身体会 只是以我眼下的水平看来,年轻代不宜大,假设年轻代大会导致转为老年代的时候,老年代撑不下.导致full gc.回收停顿时间过长