Program
#include <cstdio>
using namespace Std;
struct node
{int x;
int value;
int next;
};
Node e[60000];
int visited[1505],dis[1505],st[1505],queue[1000];
int main ()
{
int n,m,u,v,w,start,h,r,cur;
Freopen ("C.in", "R", stdin);
Freopen ("C.out", "w", stdout);
while (scanf ("%d%d", &n,&m)!=eof)
{
for (int i=1;i<=1500;i++)
{visited[i]=0;
Dis[i]=-1;
St[i]=-1; This initialization has implications for the while loop below
}
for (int i=1;i<=m;i++)
{
scanf ("%d%d%d\n", &u,&v,&w);
E[i].x=v; The record successor node is equivalent to creating a node in the linked list and causes the data field to be recorded first
E[i].value=w;
E[i].next=st[u]; Record the subscript of one of the edge table nodes of a vertex node, which is equivalent to the next pointer of the side table node in the linked list first points to his subsequent edge table node
St[u]=i; The pointer to the vertex points to the edge table node, which is equivalent to the insertion in the linked list, and the pointer of the head node changes
}
Start=1;
Visited[start]=1;
dis[start]=0;
h=0;
R=1;
Queue[r]=start;
while (H!=R)
{
H= (h+1)%1000;
CUR=QUEUE[H];
int tmp=st[cur];
visited[cur]=0;
while (Tmp!=-1)
{
if (Dis[e[tmp].x]>dis[cur]+e[tmp].value)
{
Dis[e[tmp].x]=dis[cur]+e[tmp].value;
if (visited[e[tmp].x]==0)
{
Visited[e[tmp].x]=1;
R= (r+1)%1000;
queue[r]=e[tmp].x;
}
}
Tmp=e[tmp].next;
}
}
printf ("%d\n", Dis[n]);
}
return 0;
}
- SPFA (shortest Path Faster algorithm) [graph is stored as adjacency table]
- is a queue implementation of the Bellman-ford algorithm, which reduces unnecessary redundancy calculations.
- The approximate process of the algorithm is to use a queue for maintenance. Initially, the source is added to the queue. Each time an element is taken out of the queue,
- and relaxes all points adjacent to him, and if an adjacent point relaxes successfully, it is enqueued. The algorithm ends until the queue is empty.
- It can find the shortest path from the source point to all other points in the time complexity of O (KE), and can handle negative edges.
- SPFA in the form and BFS very similar, the difference is the BFS in a point out of the queue will not be able to re-enter the queue, but SPFA
- A point may be placed in the queue again after the queue, that is, a point improved over the other points, after a period of time may be this
- The body is improved, so it is used again to improve the other points, so that repeated iteration down.
- Determine if there is a negative ring: a negative ring exists if a point enters the queue more than V (SPFA cannot process a graph with negative loops).
- The SPFA algorithm has two optimization algorithms SLF and LLL:
- Slf:small Label first policy, set the node to be added is J, team head element is I, if Dist (j) <dist (i), then J is inserted into the first team,
- Otherwise insert the tail of the team.
- Lll:large Label last policy, set the first element of the team is I, the average of all dist values in the queue is X, if dist (i) >x i insert
- At the end of the queue, find the next element until a certain I is found to make the dist (i) <=x, then the I-out pair for relaxation operation.
- Quoting online data, SLF can increase the speed of the 20%;SLF + LLL up to about 50%.
- In the practical application, the SPFA algorithm time efficiency is not very stable, in order to avoid the worst case occurrence, usually uses the more stable Dijkstra algorithm.
- */
- adjacency table storage with arrays, pnt[i,0] represents the number of nodes adjacent to I, PNT[I,1...K] stores the points adjacent to I
- int PNT[MAXN][MAXN];
- int MAP[MAXN][MAXN]; MAP[I,J] for the initial input of the distance from I to J, and map[i,i]=0; unknown map[i,j]=inf;
- int DIS[MAXN];
- Char VST[MAXN];
- int SPFA (int n,int s)
- {
- int i, PRI, end, p, t;
- memset (VST, 0, sizeof (VST));
- for (I=1; i<=n; i++)
- Dis[i] = INF;
- Dis[s] = 0;
- Vst[s] = 1;
- Q[0] = s; pri = 0; end = 1;
- while (PRI < end)
- {
- p = Q[pri];
- for (I=1; i<=pnt[p][0]; i++)
- {
- t = Pnt[p][i];
- Release first, and then decide if you want to join the queue
- if (Dis[p]+map[p][t] < dis[t])
- {
- DIS[T] = dis[p]+map[p][t];
- if (!vst[t])
- {
- q[end++] = t;
- Vst[t] = 1;
- }
- }
- }
- Vst[p] = 0;
- pri++;
- }
- return 1;
- }
SPFA algorithm Detailed