zoukankan      html  css  js  c++  java
  • 【CF edu 27 G. Shortest Path Problem?】

    time limit per test 3 seconds

    memory limit per test 512 megabytes

    input standard input

    output standard output

    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
     

    【翻译】求1 to n的异或最短路。

     
    题解:
    ①线基入门题。啊,终于入门了。
          ②最短路转树维护环这个思路很好~
    #include<stdio.h>
    #define go(i,a,b) for(int i=a;i<=b;i++)
    #define ro(i,a,b) for(int i=a;i>=b;i--)
    #define fo(i,a,x) for(int i=a[x],v=e[i].v;i;i=e[i].next,v=e[i].v)
    const int N=100003;
    struct E{int v,next,w;}e[N<<1];
    int n,m,U,V,W,head[N],k=1,d[N];bool vis[N];
    void ADD(int u,int v,int w){e[k]=(E){v,head[u],w};head[u]=k++;}
    
    struct Linear_Base
    {
    	int a[40];
    	void Insert(int x){if(x)ro(i,30,0)if((1<<i)&x)!a[i]?a[i]=x:i=0,x^=a[i];}
    	int Get_Max(int x){ro(i,30,0)if(x>(x^a[i]))x^=a[i];return x;}
    }Base;
    
    void dfs(int u)
    {
    	fo(i,head,u)!vis[v]?d[v]=d[u]^e[i].w,vis[v]=1,
    	dfs(v):Base.Insert(d[u]^d[v]^e[i].w);
    }
    
    int main()
    {
    	freopen("in.in","r",stdin);
    	scanf("%d%d",&n,&m);d[1]=0;
    	go(i,1,m)scanf("%d%d%d",&U,&V,&W),ADD(U,V,W),ADD(V,U,W);
    	dfs(1);printf("%d
    ",Base.Get_Max(d[n]));return 0;
    }//Paul_Guderian
    
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    这就你,完美的生命,悲哀而真实。——————汪峰《哭泣的拳头》
  • 相关阅读:
    补 七月九日学习报告
    七月八日学习报告
    学习笔记164—层次聚类 Hierarchical Clustering
    学习笔记163—理解模型正则化:L1正则、L2正则(理论+代码)
    学习笔记162—Linear Regression vs Logistic Regression
    学习笔记161—win7C盘 虚拟内存移动到D盘或其他盘符
    学习笔记160—文献引文分析利器 HistCite 详细使用教程
    学习笔记159—电脑word文档回车表格随文字移动
    学习笔记158—INTRODUCTION TO LINEAR MIXED MODELS
    学习笔记157—Nipype的安装
  • 原文地址:https://www.cnblogs.com/Damitu/p/7691524.html
Copyright © 2011-2022 走看看