Dijkstra and Floyd should pay attention to the heavy side.
Dijkstra
#define INF 0X3FFFFFFF
#define M-
int dist[m], map[m][m], N;
BOOL Mark[m];
void init ()
{
int i, J;
for (i = 1; I <= n; i++) //i==j can also be initialized to 0, only sometimes unsuitable for
(j = 1; J <= N; j +)
map[i][j] = inf;
}
void Dijk (int u)
{
int i, j, mins, V;
for (i = 1; I <= n; i++)
{
Dist[i] = map[u][i];
Mark[i] = false;
}
Mark[u] = true;
Dist[u] = 0; Since the map above is not 0 when i==j, this is the while
(1)
{
mins = inf;
for (j = 1; J <= N; j +)
if (!mark[j] && dist[j] < mins)
mins = Dist[j], v = j;
if (mins = = inf) break
;
MARK[V] = true;
for (j = 1; J <= N; j +)
if (!mark[j] && dist[v] + map[v][j] < Dist[j])
dist[j] = Dist[v] + map[v] [j];
}
Floyd
#define INF 0X3FFFFFFF //note, too assembly overflow
#define M //MAX points
int n, dist[m][m]; N: Actual points
void init () //Sometimes need to initialize
{
int i, J;
for (i = 1; I <= n; i++) for
(j = i + 1; j <= N; j +)
dist[i][j] = dist[j][i] = inf;
}
void Floyd ()
{
int i, j, K;
for (k = 1; k <= N; k++)
for (i = 1; I <= n; i++) for
(j = 1; J <= N; j +) //Some of the problems will overflow.
I F (Dist[i][k] + dist[k][j] < Dist[i][j])
dist[i][j] = Dist[i][k] + dist[k][j];
Spfa
#define INF 0x3fffffff
#define M //Max points
struct son{
int v, W;
Vector<son> G[m];
BOOL Inq[m]; Into the queue Mark
int dist[m], n; N: Actual points
void init ()
{for
(int i = 1; I <= n; i++)
g[i].clear ();
}
void Spfa (int u)
{
int i, V, W;
for (i = 1; I <= n; i++)
{
dist[i] = inf;
Inq[i] = false;
}
queue<int> Q;
Q.push (u);
Inq[u] = true;
Dist[u] = 0;
while (!q.empty ())
{
u = q.front ();
Q.pop ();
Inq[u] = false;
for (i = 0; i < g[u].size (); i++)
{
v = g[u][i].v;
w = G[U][I].W;
if (Dist[u] + W < Dist[v])
{
Dist[v] = Dist[u] + W;
if (!inq[v])
{
Q.push (v);
INQ[V] = true;}}}
SPFA optimization
#define INF 0X3FFFFFFF #define M 1005//MAX points struct edge{int V, W, next;} E[10005];
Estimate how many edges int pre[m], CNT, dist[m], N;
BOOL Inq[m];
Note Initialize void init () {cnt = 0;
memset (Pre,-1, sizeof (pre));
}//Note bi-directional plus-side void Addedge (int u, int v, int w)//Add-edge function, slowly simulation will understand {e[cnt].v = V;
E[CNT].W = W; E[cnt].next = Pre[u]; Replace existing side pre[u] = cnt++;
Self-inserted into the U-derived first side} void spfa (int u) {int V, W, I;
for (i = 1; I <= n; i++)//for numbering from 1 to n dist[i] = inf, inq[i] = false;
Dist[u] = 0;
Queue<int> Q;
Q.push (U);
Inq[u] = true;
while (!q.empty ()) {u = Q.front ();
Q.pop ();
Inq[u] = false;
for (i = pre[u]; I!=-1; i = e[i].next) {w = E[I].W;
v = e[i].v;
if (Dist[u] + W < Dist[v]) {Dist[v] = Dist[u] + W;
if (!inq[v]) {Q.push (v); INQ[V] = trUe
}
}
}
}
}