晚上匆忙整理了下,有时间继续!加油!
Dijkstra
void Dijkstra(){
priority_queue<pair<int,int>>q;
memset(d,0x3f,sizeof d);
memset(v,0,sizeof v);
d[1] = 0;
q.push({0,1}); //利用小根堆
while(q.size()) {
int x = q.top().second;
q.pop();
if(v[x]) continue;
v[x] = 1;
for(int i = head[x]; i ; i = Next[i]) {
int y = ver[i];
int z = edge[i];
if(d[y] > d[x] + z) {
d[y] = d[x] + z;
q.push({-d[y],y});
}
}
}
}
SPFA
void SPFA() {
queue<int>q;
memset(d,0x3f,sizeof d);
memset(v,0,sizeof v);
d[1] = 0;
v[1] = 1;
q.push(1);
while(q.size()) {
int x = q.front();
q.pop();
v[x] = 0;
for(int i = head[x]; i ; i = Next[i])
int y = ver[i];
int z = edge[i];
if(d[y] > d[x] + z)
d[y] = d[x] + z;
if(!v[y])q.push(y),v[y] = 1;
}
}
}
}
Floyd
for(int k = 1; k <= n; k ++ )
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
好简洁啊
传递闭包
for(int k = 1; k <= n; k ++ )
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
d[i][j] | = d[i][k] & d[k][j];
BF呢?
BF是什么啊……
还有这是临时整理的,还没写完
啊?bellman_ford你不知道???
哦这个知道……
大佬简写BF不知道……
还有SPFA不就是Bellman-Ford的优化吗。。。
是的,但是bellman-ford可以处理“有边数限制的最短路”