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;
    }

  • 相关阅读:
    【C语言入门教程】5.1 函数说明 与 返回值
    【C语言入门教程】4.10 综合实例
    【C语言入门教程】4.9 指向指针的指针
    【C语言入门教程】4.8 指针数组
    【C语言入门教程】4.7 指针的地址分配
    Windows 7 共享文件夹 给 VirtualBox 中的 Ubuntu 14
    【C语言入门教程】4.6 指针 和 数组
    Ubuntu 12/14 个性化配置
    【C语言入门教程】4.5 指针变量的定义与引用
    【C语言入门教程】4.4 指针 与 指针变量
  • 原文地址:https://www.cnblogs.com/csushl/p/9386781.html
Copyright © 2011-2022 走看看