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. ...
随机推荐
- Linux命令实战(四)
1.Linux上的文件管理类命令都有哪些,其常用的使用方法及相关示例演示. 文件或目录的新建 touch :将每个文件的访问时间和修改时间修改为当前时间.若文件不存在将会创建为空文件,除非使用-c或- ...
- 接口测试专题(Java & jmeter & Linux基础)
以下是我和两个朋友原创文章合集,主题是接口测试,有Java接口测试案例和jmeter的案例,还有接口测试相关服务器操作基础.欢迎点赞.关注和转发. 接口测试 httpclient处理多用户同时在线 h ...
- 如何基于k8s快速搭建TeamCity(YAML分享)
前言 最近有朋友基于之前的博客<Docker最全教程之使用TeamCity来完成内部CI.CD流程(十七)>搭建TeamCity时出现了一些问题,由于平常比较忙,没有及时答复,非常抱歉. ...
- Flutter 构建的 Mac 桌面应用上无法发出网络?
在上一篇文章中我们分享了,如何开发桌面应用.在本章文章中,来解决一下为何在 Mac 中无法发出网络情况的原因. 起因 事情起因是这样的:我总觉得写一个 Demo 不足以体现我们开发同学的能力.直到最 ...
- 【Elasticsearch 7 探索之路】(三)倒排索引
上一篇,我们介绍了 ES 文档的基本 CURE 和批量操作.我们都知道倒排索引是搜索引擎非常重要的一种数据结构,什么是倒排索引,倒排索引的原理是什么. 1 索引过程 在讲解倒排索引前,我们先了解索引创 ...
- thinkphp 6.0 在 initialize 中重定向无效
thinkphp 6.0 在 initialize 中重定向无效 改用 header() 函数 实例: // header('location:/index.php/模块/控制器/方法'); head ...
- nyoj 26-孪生素数问题(打表)
26-孪生素数问题 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:10 submit:43 题目描述: 写一个程序,找出给出素数范围内的所有孪生素数 ...
- 🔥《手把手》系列基础篇之2-python+ selenium-打开和关闭浏览器(详细)
1. 简介 本节介绍如何初始化一个webdriver实例对象driver,然后打开和关闭firefox浏览器.要用selenium打开fiefox浏览器.首先需要去下载一个driver插件geckod ...
- spring、mybatis、事务项目整合,附完整代码和数据库文件
配置依赖项 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...
- 堆 堆排序 优先队列 图文详解(Golang实现)
引入 在实际应用中,我们经常需要从一组对象中查找最大值或最小值.当然我们可以每次都先排序,然后再进行查找,但是这种做法效率很低.哪么有没有一种特殊的数据结构,可以高效率的实现我们的需求呢,答案就是堆( ...