SPFA algorithm for finding the shortest path

SPFA use algorithms to solve hdu1874

First look at SPFA related concepts: https://baike.so.com/doc/6843451-7060772.html

SPFA can handle negative right side

If given a negative view of the right side, a similar algorithm Dijkstra algorithm will be no useless, he sent in handy on SPFA algorithm. Brevity, we agreed weighted G is absent to FIG negative cycles, i.e. the shortest path must exist. Shortest path estimated value of each node by recording an array of d, and storing adjacency table G. FIG. Our approach is dynamic approximation: the establishment of a FIFO queue to be optimized to save nodes, each node u remove the head of the queue optimization, and with u point of the current estimate of the shortest path to leave the point u points to the node v relaxation operation, if the point v shortest path estimation value be adjusted, and the point v is not currently in the queue, the point will be placed in the tail v. Such continuously removed from the queue node relaxation operation is performed until the queue is empty.

Example hdu1874:

The meaning of problems

Since the province of smooth implementation of the project for many years, finally built many roads. But more is not a good way, each time from one town to another, there are many kinds of road schemes to choose from, and some programs are other programs, walking distance much shorter than others. This allows pedestrians very troubled. 

Now, start and end points are known, you calculate from the beginning to the end, how much the shortest distance required to walk.

Input this topic contains multiple sets of data, the processing to the end of the file. 
Each test line contains two positive integers N and M (0 <N <200,0 < M <1000), represents the number of towns and the number of existing roads have been built. In urban numbered 0 ~ N-1. 
Followed by M lines road information. Each row contains three integers A, B, X (0 < = A, B <N, A! = B, 0 <X <10000), expressed between towns and urban A B X a length of two-way road. 
The next line followed by two integers S, T (0 <= S , T <N), representing the start and end points. Output For each set of data, the output from the minimum required to walk on one line. If the route from S to T does not exist, the output of -1. 
The Sample the Input

3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

2
-1

#include<cstdio>
#include<queue>
using namespace std;
const int INF=1000000;
const int MAXN=200+10;
int n,m;
int map[MAXN][MAXN];
int dis[MAXN];
void SPFA(int s)
{
    for(int i=0;i<n;i++)
        dis[i]=INF;
    bool vis[MAXN]={0};
    vis[s]=true;
    dis[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        vis[cur]=false;
        for(int i=0;i<n;i++)
        {
            if(dis[cur] + map[cur][i] < dis[i])
            {
                dis[i]=dis[cur] + map[cur][i];
                if(!vis[i])
                {
                    q.push(i);
                    vis[i]=true;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                map[i][j]=INF;
        for(int i=0;i<m;i++)
        {
            int from,to,dis;
            scanf("%d%d%d",&from,&to,&dis);
            if(map[from][to]>dis)
                map[from][to]=map[to][from]=dis;
        }
        int s,t;
        scanf("%d%d",&s,&t);
        SPFA(s);
        if(dis[t]==INF)
            puts("-1");
        else
            printf("%d\n",dis[t]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LJHAHA/p/11231515.html
Recommended