Ford algorithm (single source Shortest path)

Source: Internet
Author: User

Advantages: The code is small, the complexity is not high, you can determine whether there will be negative ring.

Cons: Low efficiency.

The algorithm illustrates:

The idea of this algorithm is very simple, first of all, it is based on the line from the beginning to connect with it to start the refresh, as long as the path to meet the refresh is smaller than the original path, then immediately update this data, is this data as a new number

According to At the same time this algorithm has a very important advantage, that is to be able to determine whether there is a negative ring.


Negative Ring Judging principle:

The code of this algorithm I will have a description of the code below, the algorithm is a whole update to achieve the shortest path, then there is a question about the number of updates to this algorithm, first of all to determine

Down the vertex does it make an update around it will update at least one vertex? Answer: Yes, if a definite vertex is bound to be able to update another determined vertex, here is the definite vertex that is the vertex

With a definite path. For example: A->z This inside a is the starting point, if a beginning can not be determined with its vertex is not connected to the shortest point, then this a->z also have no solution, on this issue I will no longer elaborated.


Code:

#include <iostream> #include <cstdio>using namespace std;typedef struct line{    int first;    int last;    int length;} L L a[50];int D[50];int Main () {    int n,m,temp;//n is the number of edges, M is the number of vertices    scanf ("%d%d", &n,&m);    Fill (d,d+50,8888888),//Assign the D all to 8888888 first, this is to let them first as I have not fixed the value of the vertex!    d[1]=0;//starting point is 0 for    (int i=0;i<n;i++) scanf ("%d%d%d", &a[i].first,&a[i].last,&a[i].length);    for (int i=1;i<=m;i++)    {        temp=1;//Use this variable to determine if the ring is the shortest path! for        (int j=0;j<n;j++)        {            if (d[a[j].last]>d[a[j].first]+a[j].length)//judgment standard            {                d[a[j].last]=d[a[j].first]+a[j].length;                Temp=0;                if (i==m) {//above already has an explanation for                    printf ("There is a negative ring!!!!! ");                    Return-1        ;        }}} if (temp) {break            ;        }    }    for (int i=1;i<=m;i++)    {        printf ("%d\n", D[i]);}    }


Ford algorithm (single source Shortest path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.