「luogu4366」最短路
2024-10-08 14:24:59
「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;
}
最新文章
- SQLAlchemy文档翻译
- 【Swift学习】Swift编程之旅---ARC(二十)
- putty 访问 vmware中ubuntu 方法
- 基于矩阵模式的 Web 软件测试手段(转)
- java web 学习十五(jsp基础语法)
- 从lambda到函数式编程
- 4、Hbase
- ios 调节器 modal 得知
- angularjs-1.3代码学习 模块
- explorer.exe 该文件没有与之关联的程序来执行该操作
- 总结各类错误(always online)
- ArcMap插件开发初识:Add In
- Scrapy-redis<;数据库篇>;
- 11.1-uC/OS-III就绪列表
- c++ 如何编写接口类(interface)
- 向量运算 与 JavaScript
- jQuery UI 给button添加ID
- Spring与web MVC的整合——Spring的应用上下文管理
- 《Programming with Objective-C》第七章 Values and Collections
- web.xml执行顺序
热门文章
- AC自动机讲解超详细
- Apache的虚拟主机功能(基于IP地址、基于虚拟主机、基于端口)
- 分布式事务 --- 2PC 和 3PC
- 基于SILVACO ATLAS的a-IGZO薄膜晶体管二维器件仿真(08)
- 基于SILVACO ATLAS的a-IGZO薄膜晶体管二维器件仿真(04)
- PCC值average pearson correlation coefficient计算方法
- Spring 的 Bean 生命周期,11 张高清流程图及代码,深度解析
- 无刷新的批量图片上传插件.NET版
- Maven中配置jdk的版本
- border-1px的实现(stylus)如何在移动端设置1px的border