zoukankan      html  css  js  c++  java
  • 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 ≤ 100000n - 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 ≤ n0 ≤ 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;
    }

  • 相关阅读:
    React组件二
    React组件一
    React新接触
    清除浮动的方法
    div section article aside的理解
    html引入外部的jswenjian
    绘制扇形,空心文字,实心文字,颜色线性 放射性渐变
    绘制扇形空心 实心文字 ,颜色线性渐变,颜色放射性渐变
    绘制圆弧的几种简单方法
    求两个有序数组的中位数
  • 原文地址:https://www.cnblogs.com/csushl/p/9386519.html
Copyright © 2011-2022 走看看