【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\n",Base.Get_Max(d[n]));return 0;
- }//Paul_Guderian
- 这就你,完美的生命,悲哀而真实。——————汪峰《哭泣的拳头》
【CF edu 27 G. Shortest Path Problem?】的更多相关文章
- Codefroces Educational Round 27 845G Shortest Path Problem?
Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...
- 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码
00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...
- 线性基【CF845G】Shortest Path Problem?
Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...
- Codeforces 845G Shortest Path Problem?
http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...
- 【cf contest 1119 G】Get Ready for the Battle
题目 你有\(n\)个士兵,需要将他们分成\(m\)组,每组可以为0: 现在这些士兵要去攻打\(m\)个敌人,每个敌人的生命值为\(hp_i\) : 一轮游戏中一组士兵选定一个攻打的敌人,敌人生命值- ...
- 【Cf Edu #47 G】Allowed Letters
这个题大概就是每一个位置都有一个能填字符的限制(一个点集),给出已有的$n$个字符,问能填出的最小字典序的字符串. 总体思路是贪心,每一位尽量选最小的字符. 关键在于判断在某位选了一个字符后,接下来的 ...
- [CF845G]Shortest Path Problem?
题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...
- cf 843 D Dynamic Shortest Path [最短路+bfs]
题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...
- 题解 CF938G 【Shortest Path Queries】
题目让我们维护一个连通无向图,边有边权,支持加边删边和询问从\(x\)到\(y\)的异或最短路. 考虑到有删边这样的撤销操作,那么用线段树分治来实现,用线段树来维护询问的时间轴. 将每一条边的出现时间 ...
随机推荐
- Linux帮助都有哪几种,如何使用?
帮助文件有2类,内置命令和外部命令. 其中内置命令就是shell内核自带的,因为shell当中自己要进行管理,那么就需要一些命令进行管理,不同的shell肯定有不同的shell命令,我们用type命令 ...
- Apache安装之后,在浏览器输入ip无法访问
博主本来在linux下面配置安装了apache,然后用浏览器输入ip却无法访问 就一直在想是不是dns无法解析的问题,最后才发现原来是防火墙的原因, 在linux下面 service iptables ...
- POJ-3126 BFS,埃式筛选及黑科技
题目大意:给定两个四位素数a b,要求把a变换到b,变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的 ...
- [Bzoj2246]迷宫探险(概率+DP)
Description 题目链接 Solution 用三进制表示陷阱状态,1表示有害,2表示无害,0表示不知道 用\(f[S][i]\)表示状态为S时陷阱i有害的概率,这个可以预处理出 \(d[S][ ...
- 笔记-scrapy-去重
笔记-scrapy-去重 1. scrapy 去重 scrapy 版本:1.5.0 第一步是要找到去重的代码,scrapy在请求入列前去重,具体源码在scheduler.py: def en ...
- hadoop,hbase,hive
linux上安装hadoop,然后安装hbase,然后安装zookeeper,最后安装hive.hbase安装在hdfs下.hive是纯逻辑表,hbase是物理表.hdfs是hadoop上的一个组件.
- centos使用--防火墙
目录 1 切换到zsh 1.1 查看系统当前的shell 1.2 查看bin下是否有zsh包 1.3 安装zsh包 1.4 切换shell至zsh 2 安装oh-my-zsh 2.1 oh-my-zs ...
- js常用框架
JS常用框架:jQuery.Prototype.MooTools 参考:w3cshool jQuery jQuery 是目前最受欢迎的 JavaScript 框架. 它使用 CSS 选择器来访问和操作 ...
- IIS 部署网站--浏览--“该页无法显示”
解决办法: 打开IIS管理器--web站点(网站)--右键点击对应的站点--属性--主目录--执行权限改为(脚本和和执行文件) 点击“应用”--确定. 重启一下站点,OK.
- 给vmstat加上时间戳
vmstat -n 5 | awk '{print strftime("[%Y-%m-%d %H:%M:%S]"),$0}' 或者 vmstat -n 5 | awk '{ pri ...