Codeforces 845 G Shortest Path Problem?
来源:互联网 发布:java工程师如何考试 编辑:程序博客网 时间:2024/06/06 00:31
题目地址
题意:给你一个图,求1~n的最短路(但是权重的计算方式变成了异或)
思路:如果没有环的存在的话那就是直接算下去,但是正是因为有环的存在,所以在一些地方可能绕环一周以后的值更小。所以要记录下来,最后判断最小的权重是多少。解释一下为什么要记录的是:add(nums^mapp[u[i].val^num[v]);
吧。因为其实num[v]等于原来到v的距离,所以包括了之前的值,但是之前的值在记录中是要去掉的,所以直接以后就把这些值给去掉了,那剩下的应该就是图中画红线的部分了。
#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#define N 100010#define M 90010#define LL __int64#define inf 0x3f3f3f3f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const LL mod = 1e9 + 7;struct node { int to; LL val; node(int a, LL b) { to = a; val = b; }};vector<node> mapp[N];vector<LL> v;bool vis[N];LL num[N];int n, m;void init() { for (int i = 0; i <= n; i++) { mapp[i].clear(); } memset(num, 0, sizeof(num)); memset(vis, false, sizeof(vis)); v.clear();}void add(LL x) { for (int i = 0; i < v.size(); i++) { x = min(x, x^v[i]); } if (x) { v.push_back(x); }}void dfs(int u, int nums) { vis[u] = true; num[u] = nums; for (int i = 0; i < mapp[u].size(); i++) { int v = mapp[u][i].to; if (vis[v]) {//如果有环的话,把另一种情况加入 add(nums^mapp[u][i].val^num[v]); } else { dfs(v, nums^mapp[u][i].val); } }}int main() { cin.sync_with_stdio(false); int a, b; LL c; while (cin >> n >> m) { init(); for (int i = 0; i < m; i++) { cin >> a >> b >> c; mapp[a].push_back(node(b, c)); mapp[b].push_back(node(a, c)); } dfs(1, 0); for (int i = 0; i < v.size(); i++) { num[n] = min(num[n], num[n] ^ v[i]); } cout << num[n] << endl; } 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
- Hibernate配置文件不在默认目录时的路径问题
- 算法提高 ADV-89 输出九九乘法表
- 算法提高 ADV-90 输出日历
- java进行qq邮箱邮件发送
- STM32连接Marvell交换芯片88E6176的PHY
- Codeforces 845 G Shortest Path Problem?
- POJ 2241 The Tower of Babylon(DP)
- 算法提高 ADV-91 素数判断
- 虚拟机中centOS上网
- mybatis中mapper绑定异常
- 算法提高 ADV-92 求最大公约数
- 读论文《Distributed Representations of Words and Phrases and their Compositionality》
- react-redux 小例子
- phpstorm + xdebug远程调试