Codeforces 845G Shortest Path Problem
来源:互联网 发布:淘宝上怎么搜漆弹枪 编辑:程序博客网 时间:2024/05/16 12:32
原题
codeforces845G
题意
给一个无向带权图,求1-n的异或最短路。
解题思路
因为我们是在异或的情况下求最短路,因此环是一个特殊的存在。
我们从一个点走向环,在环内绕一圈再回到当前点,我们只会获得环的权值。也就是说,对于一个环的权值,我们可以只考虑选与不选。
如果我们在dfs遍历整个图的过程中,拆掉回边,图就成了一棵树。我们可以记录下i号点到1号点的异或距离d[i],那么任意两个点u、v之间的距离可以用d[u]^d[v]表示。
假设在dfs的时候我们找到了一条连接u、v,权值为x的回边,相当于我们就找到了一个权值为d[u]^d[v]^x的环。
将环的权值放入线性基,答案相当于就是将d[n]与线性基内的数异或能取到的最小值。
代码
#include <cstdio>using namespace std;const int N=1e5+10;struct Edge{ int to,next,cost; } way[N<<1];int n,m,tot,num[N];bool vis[N];struct Base{ int x[31]; void Insert(int v) { if (!v) return ; for (int i=30;i>=0;--i) if ((1<<i)&v) { if (x[i]) v^=x[i]; else { x[i]=v; break; } } } int Query(int v) { for (int i=30;i>=0;--i) if ((v^x[i])<v) v^=x[i]; return v; }}base; void AddEdge(int a,int b,int c) { way[++tot]=(Edge){b,num[a],c}; num[a]=tot; }void Init(){ scanf("%d%d",&n,&m); int u,v,cost; for (int i=1;i<=m;++i) { scanf("%d%d%d",&u,&v,&cost); AddEdge(u,v,cost); AddEdge(v,u,cost); }}int dis[N];void Dfs(int x){ vis[x]=1; for (int i=num[x];i;i=way[i].next) { int v=way[i].to; if (!vis[v]) { dis[v]=dis[x]^way[i].cost; Dfs(v); }else base.Insert(dis[v]^dis[x]^way[i].cost); }}void Solve(){ Dfs(1); printf("%d\n",base.Query(dis[n]));}int main(){ Init(); Solve(); return 0;}
阅读全文
0 0
- Codeforces 845 G Shortest Path Problem?
- Codeforces 845G Shortest Path Problem
- Educational Codeforces Round 27 G. Shortest Path Problem?
- Codeforces 845G Shortest Path Problem?(异或最短路)
- Educational Codeforces Round 27-搜索&技巧&异或-G. Shortest Path Problem?
- shortest path problem
- 状态压缩DP joboj1430 shortest path 和 codeforces Problem 21 D Traveling Graph
- cf#3A Shortest path of the kinghttp://codeforces.com/contest/3/problem/A
- 算法学习和实践 ---- Shortest Path Problem
- HDU Problem 5636 Shortest Path 【Floyd】
- CodeForces 59E Shortest Path 用边跑最短路
- codeforces 45G Prime Problem
- Codeforces contest 802 problem G
- Single-source shortest path problem: SPFA vs. Dijkstra
- Shortest Path
- Shortest Path
- Shortest Path
- codeforces 3A - Shortest path of the king
- 毕业游
- 欢迎使用CSDN-markdown编辑器
- Ubuntu -- 更换 14.04 安装源
- Ubuntu -- 14.04 安装 LAMP 环境
- linux安装软件时提示找不到镜像的问题:Couldn't resolve host 'mirrorlist.centos.org'
- Codeforces 845G Shortest Path Problem
- 开发 -- phpStrom 安装与远程访问使用
- Ubuntu -- ubuntu14.04 利用nginx做虚拟主机
- 数字地址
- Python
- hibernate 多对多关系
- Ubuntu -- Ubuntu 16.04建立热点(安卓可连接)
- Ubuntu -- Ubuntu 16.04 截屏功能
- Ubuntu -- Ubuntu系统问题处理大全(持续 更新)