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

  • 相关阅读:
    背包问题
    基本TCP套接字编程
    P中值选址问题的整数规划求解
    Leapms + cplex解决 混合整数规划问题
    如何用整数规划求解NP完全问题
    用Leapms建摸 / 用 CPLEX 求解 旅行商问题 整数规划问题
    用线性规划建模(确定参数)关键路径法
    流水车间调度算法分析的简单+Leapms实践--混合整数规划的启发式建模
    Wolsey "强整数规划“ 建模的+Leapms实践——无产能批量问题
    Wolsey“强整数规划模型”经典案例之一单源固定费用网络流问题
  • 原文地址:https://www.cnblogs.com/csushl/p/9386519.html
Copyright © 2020-2023  润新知