SPFA(Shortest Path Faster Algorithm)(队列优化)算法是求单源最短路径的一种算法,它还有一个重要的功能是判负环(在差分约束系统中会得以体现),在Bellman-ford算法的基础上加上一个队列优化,减少了冗余的松弛操作,是一种高效的最短路算法。

spfa的算法思想 —— 动态逼近法

​设立一个先进先出的队列q用来保存待优化的结点,优化时每次取出队首结点u,并且用u点当前的最短路径估计值对离开u点所指向的结点v进行松弛操作,如果v点的最短路径估计值有所调整,且v点不在当前的队列中,就将v点放入队尾。这样不断从队列中取出结点来进行松弛操作,直至队列空为止。

​松弛操作的原理是著名的定理:“三角形两边之和大于第三边”,在信息学中我们叫它三角不等式。所谓对结点i,j进行松弛,就是判定是否dis[j]>dis[i]+w[i,j],如果该式成立则将dis[j]减小到dis[i]+w[i,j],否则不动。

代码采用c++中STL模板(queue + vector), 减少不必的空间开销以及提高代码易读性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <queue>
#include <vector>
#include <array>

const int N = 20000 + 10;

std::queue<int> q;
auto next = std::vector<std::vector<int>>(N);
auto val = std::vector<std::vector<int>>(N);
std::array<bool, N> vis;
std::array<long long, N> dis;
int n, m, start = 1;

void input();

void init();

void spfa() {
while (!q.empty()) {
int tem = q.front();
q.pop();
for (int i = 0; i < next[tem].size(); i++) {
if (dis[next[tem][i]] > dis[tem] + val[tem][i]) {
dis[next[tem][i]] = dis[tem] + val[tem][i];
if (!vis[next[tem][i]]) {
vis[next[tem][i]] = true;
q.push(next[tem][i]);
}
}
}
}
for (int i = 1; i <= n; i++)
std::cout << dis[i] << std::endl;
}

int main() {
input();
init();
spfa();
return 0;
}

void input() {
std::cin >> n >> m; // n 个点, m条边
for (int i = 1; i <= m; i++) {
int a, b, t; // a -> b 存在一条距离为 t 的边
std::cin >> a >> b >> t;
next[a].push_back(b);
val[a].push_back(t);
}
}

void init() {
vis.fill(false);
dis.fill(0x3f3f3f3f);
dis[start] = 0;
q.push(start);
vis[start] = true;
}

Python 3.6.1

  • 下载地址python-3.6.1-amd64.exe
  • 系统需求:64位/32位
  • 备注:python需加入系统path,并能够以python链接到

Mongodb 3.4.4

数据库文件目录: dbpath=C:/mongoDB3.4.4/data

日志目录: logpath=C:/mongoDB3.4.4/log/mongo.log diaglog=3

  • 安装服务至系统服务
1
mongod –config C:\mongoDB3.4.4\bin\mongodb.config –install
  • 启动与关闭mongoDB:
1
2
net start MongoDB
net stop MongoDB
  • 查看aishub数据库状态命令:
1
2
3
mongo 
use aishub
db.status()
  • 数据导出命令(默认csv格式):
1
mongoexport -d aishub -c sheet1 -o c:\data.csv
  • 数据集合(sheet1)删除
1
use aishubdb.sheet1.drop()

Python 依赖包

Pip 国内源

阿里云 http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

豆瓣 http://pypi.douban.com/simple/

清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/pip

pip使用

后面加上-i参数,指定pip源

1
pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

Pip更新

1
python -m pip install –upgrade pip 

依赖包

  • beautifulsoup4
  • requests
  • pymongo
  • lxml

这是smile先生的第一个博客

我不希望她 被废弃

我会好好的 维护她

正如 《我的梦》

借用自己的空闲时间

实现自己的 小小梦想

歌曲:Cymatics*

歌手:Cymatics

所属专辑: Solar Echoes

cymatics是使声音形象化的过程,基本上借由沙或水等媒介的振动来达成。——百度百科

这才是电音该有的样子

旋律和节奏穿过物理的障碍

透过波形和几何

把宇宙和神的秘密射给你

直达肉眼凡胎

这是音乐的神祇

源自心灵的电波

和他人无关

原点即是本我的自己

MV外链:http://music.163.com/#/mv?id=375405

0%