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 ≤ 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
3 3
1 2 3
1 3 2
3 2 0
2
2 2
1 1 3
1 2 3
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;
}
CodeForces845G-Shortest PathProblem?的更多相关文章
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Leetcode: Encode String with Shortest Length && G面经
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- LeetCode 214 Shortest Palindrome
214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- POJ2001 Shortest Prefixes
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- idea 常用功能
Ctrl + E:打开最近文件 双击 Shift:按文件名查找文件 Ctrl + Shift + F:全局搜索 Alt + ~(数字 1 左边的键):commit.push 代码 ...
- 解决vuex的数据刷新(F5)后会被初始化的问题
介绍一个vuex的数据刷新(F5)后会被初始化的问题处理的插件:vuex-localstorage 实现的原理大概就是监听浏览器的刷新,关闭事件,把vuex的值存储到本地localstorage,刷新 ...
- 如何基于 PHP-X 快速开发一个 PHP 扩展
0x01 起步 PHP-X本身基于C++11开发,使用cmake进行编译配置.首先,你需要确定所有依赖项已安装好.包括: gcc-4.8 或更高版本 PHP7.0 或更高版本,需要php7-dev 开 ...
- nyoj 40-公约数和公倍数(gcd)
40-公约数和公倍数 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:30 submit:47 题目描述: 小明被一个问题给难住了,现在需要你帮帮忙. ...
- ubuntu18 拨号连接宽带网络方法
1.打开终端查看以太网网卡编号 2.打开/usr/share/applications/ 3.点击network connections 4.点击左下角的+号 5.连接类型选择DSL/PPPoE 6. ...
- mysql数据库E-R图
学会绘制E-R图 绘制E-R图首先要了解什么是实体,什么是属性,什么是联系. 1.首先实体是指现实世界中具有区分其他事物的特征或属性与其他实体有联系的实体,针对于数据库中的表而言实体是指表中一行一行特 ...
- 程序员修神之路--kubernetes是微服务发展的必然产物
菜菜哥,我昨天又请假出去面试了 战况如何呀? 多数面试题回答的还行,但是最后让我介绍微服务和kubernetes的时候,挂了 话说微服务和kubernetes内容确实挺多的 那你给我大体介绍一下呗 可 ...
- Springboot操作Elasticsearch
常见的日志系统是基于logstach+elasticsearch+kibna框架搭建的,但是有时候kibana的查询无法满足我们的要求,因此有时需要代码去操作es,本文后续都以es代替elastics ...
- python3 之 面向对象(类)、继承、派生和多态
类提供了一种 组合数据和功能 的方法.创建一个新类意味着:创建一个新 类型 的对象,从而允许创建一个该类型的新 实例. 每个类的实例可以拥有: 保存自己状态的属性. 一个类的实例也可以有改变自己状态 ...
- 跑健壮性Monkey,出现一次Crash全过程-日志分析-Dotest董浩
最近带着学生做的某个项目,跑健壮性Monkey,出现一次Crash全过程-日志分析: 准备:搭建adb环境.安装实际测试包:开始: Monkey命令: adb shell monkey -p com. ...