# 【清明】最短路

//图上DP

floyd//多源最短路
spfa
spfa_slf（双端队列优化
dijkstra
dijkstra_heap（堆优化

//其实搜索也行2333//被打飞
0.无权图

@BFS 遍历全图，分层，ans = 层数*权值；
DFS不行 举个例子：

1.floyd

void floyd()
{
for(int k = 1; k <= n; k ++)
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
dis[i][j] = min(dis[i][j],dis[i][k] + dis[k][j]);
}

2.spfa /spfa_slf

spfa可以判负环

spfa:

queue < int > q;
void spfa(int s){
while(!q.empty()) q.pop();
memset(use,0,sizeof(use));
memset(dis,0x3f,sizeof(dis));
use[s] = true; dis[s] = 0;
q.push(s);

while(!q.empty()){
int u = q.front(); q.pop();
use[u] = false;
for(int i = first[u]; i != -1; i = nxt[i]){
int w = l[i].t;
if(dis[w] > dis[u] + l[i].v){
dis[w] = dis[u] + l[i].v;
if(use[w]) continue;
q.push(w); use[w] = true;
}
}
}
return;
}

spfa_slf

deque < int > q;
void spfa(int s){
while(!q.empty()) q.pop_front();
memset(use,0,sizeof(use));
memset(dis,0x3f,sizeof(dis));
use[s] = true; dis[s] = 0;
q.push_front(s);

while(!q.empty()){
int u = q.front(); q.pop_front();
use[u] = false;
for(int i = first[u]; i != -1; i = nxt[i]){
int w = l[i].t;
if(dis[w] > dis[u] + l[i].v){
dis[w] = dis[u] + l[i].v;
if(use[w]) continue;
if(q.empty() || dis[w] < dis[q.front()]) q.push_front(w);
else q.push_back(w);
}
}
}
return;
}


/*

*/

3.dij/dij_heap

int dij(int s,int e)
{
memset(use,0,sizeof(use));
while(!q.empty())   q.pop();
q.push((re){s,0});
while(!q.empty())
{
re us = q.top();
q.pop();
int u = us.u;
if(u == e)  return us.v;
if(use[u])  continue;
use[u] = true;
for(int i = first[u]; i != -1; i = next[i])
{
int w = l[i].t;
int rs = us.v + l[i].v;
q.push((re){w,rs});
}
}
return -1;
}