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 xy 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

这是一个求从地点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[n];
    for(int i=31;i>=0;i--) ans=min(ans,ans^b[i]);
    cout<<ans<<endl;
    return 0;
}

CodeForces845G-Shortest PathProblem?的更多相关文章

  1. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  2. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  3. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  4. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  5. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  6. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Leetcode: Encode String with Shortest Length && G面经

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  8. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  9. POJ2001 Shortest Prefixes

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  10. Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. 【algo&ds】3.栈和队列

    1.堆栈 堆栈(Stack):具有一定操作约束的线性表(只在一端(栈顶,Top)做插入.删除) 先进后出特性 1.1堆栈的抽象数据类型描述 类型名称: 堆栈(Stack) 数据对象集:一个有0个或多个 ...

  2. 《Java多线程面试题》系列-创建线程的三种方法及其区别

    1. 创建线程的三种方法及其区别 1.1 继承Thread类 首先,定义Thread类的子类并重写run()方法: package com.zwwhnly.springbootaction.javab ...

  3. spark集群搭建(三台虚拟机)——hadoop集群搭建(2)

    !!!该系列使用三台虚拟机搭建一个完整的spark集群,集群环境如下: virtualBox5.2.Ubuntu14.04.securecrt7.3.6_x64英文版(连接虚拟机) jdk1.7.0. ...

  4. secureCRT连接虚拟机

    1.secureCRT英文版下载 链接:https://pan.baidu.com/s/1LFWD-k2r4ZB7DHQA66QogQ 密码:khmo 破解方式参考 2.虚拟机静态IP设置 参考 3. ...

  5. Zabbix-(五)监控Docker容器与自定义jvm监控项

    Zabbix-(五)监控Docker容器与自定义jvm监控项 一.前言 前文中讲述了Zabbix对服务器硬件方面的监控功能,本文将讲述利用Zabbix监控Docker容器中的Java Web服务,并通 ...

  6. python:沙盒(virtualenv)

    当电脑需要使用多个版本的python时,可以使用沙盒:或者使用docker: virtualenv是Python自带的,通过pip安装的 [root@centos7 public]# cd jinji ...

  7. UCACO刷题

    UCACO刷题 SUBMIT: /* ID: your_id_here LANG: C++ TASK: test */ 文件:freopen(“file.in", "r" ...

  8. Oracle10g安装步骤(二)

    接上篇:

  9. 2019-10-29:渗透测试,基础学习,sqlmap文件读取,写入,dnslog盲注作用,mssql手工注入,笔记

    sqlmap参数--file-read,从数据库服务器中读取文件--file-write,--file-dest,把文件上传到数据库服务器中 dnslog平台的学习和它在盲注中的应用1,判断注入点2, ...

  10. Tensorflow常用函数说明

    1.矩阵操作 1.1矩阵生成 这部分主要将如何生成矩阵,包括全0矩阵,全1矩阵,随机数矩阵,常数矩阵等 sess=tf.InteractiveSession() #x=tf.ones([2,3],tf ...