「luogu4366」最短路

传送门

直接连边显然不行,考虑优化。

根据异或的结合律和交换律等优秀性质,我们每次只让当前点向只有一位之别的另一个点连边,然后就直接跑最短路。

注意点数会很多,所以用配对堆优化 \(\text{Dijkstra}\) 即可。

参考代码:

#include <cstring>
#include <cstdio>
#include <ext/pb_ds/priority_queue.hpp>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
using namespace __gnu_pbds;
const int _ = 1e6 + 10, __ = 3e6 + 10; int tot, head[_], nxt[__], ver[__], w[__];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; } int n, m, c, s, t, dis[_], vis[_];
struct node { int val, u; } ;
inline bool operator < (const node& x, const node& y) { return x.val > y.val; }
priority_queue < node > Q; inline void Dijkstra() {
memset(dis, 0x3f, sizeof dis);
Q.push((node) { 0, s }), dis[s] = 0;
while (!Q.empty()) {
int u = Q.top().u; Q.pop();
if (vis[u]) continue ; vis[u] = 1;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[v] > dis[u] + w[i])
dis[v] = dis[u] + w[i], Q.push((node) { dis[v], v });
}
}
} int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(n), read(m), read(c);
for (rg int u, v, d; m--; )
read(u), read(v), read(d), Add_edge(u, v, d);
for (rg int i = 1; i <= 100000; ++i)
for (rg int j = 0; j <= 16; ++j) Add_edge(i, i ^ (1 << j), (1 << j) * c);
read(s), read(t);
Dijkstra();
printf("%d\n", dis[t]);
return 0;
}

最新文章

  1. SQLAlchemy文档翻译
  2. 【Swift学习】Swift编程之旅---ARC(二十)
  3. putty 访问 vmware中ubuntu 方法
  4. 基于矩阵模式的 Web 软件测试手段(转)
  5. java web 学习十五(jsp基础语法)
  6. 从lambda到函数式编程
  7. 4、Hbase
  8. ios 调节器 modal 得知
  9. angularjs-1.3代码学习 模块
  10. explorer.exe 该文件没有与之关联的程序来执行该操作
  11. 总结各类错误(always online)
  12. ArcMap插件开发初识:Add In
  13. Scrapy-redis&lt;数据库篇&gt;
  14. 11.1-uC/OS-III就绪列表
  15. c++ 如何编写接口类(interface)
  16. 向量运算 与 JavaScript
  17. jQuery UI 给button添加ID
  18. Spring与web MVC的整合——Spring的应用上下文管理
  19. 《Programming with Objective-C》第七章 Values and Collections
  20. web.xml执行顺序

热门文章

  1. AC自动机讲解超详细
  2. Apache的虚拟主机功能(基于IP地址、基于虚拟主机、基于端口)
  3. 分布式事务 --- 2PC 和 3PC
  4. 基于SILVACO ATLAS的a-IGZO薄膜晶体管二维器件仿真(08)
  5. 基于SILVACO ATLAS的a-IGZO薄膜晶体管二维器件仿真(04)
  6. PCC值average pearson correlation coefficient计算方法
  7. Spring 的 Bean 生命周期,11 张高清流程图及代码,深度解析
  8. 无刷新的批量图片上传插件.NET版
  9. Maven中配置jdk的版本
  10. border-1px的实现(stylus)如何在移动端设置1px的border