Dijkstra算法-用于求单源最短路径

Dijkstra算法




#include <iostream>

using namespace std;

#define MaxLine 9999

 struct Node{

    int node;

    int value;

    Node*next;

};

#define InISize 20

#define AppSize 50

typedef struct Stack{

    int Capacity;

    Node*base;

    Node*top;

}Stack;

typedef struct queue{

    Node *rear;

    Node *front;

    int size;

}Queue;

void IniStack(Stack *s)

{

    s->base=(Node*)malloc(InISize*sizeof(Node));

    if(!s->base)

        return ;

    s->top=s->base;

    s->Capacity=InISize;

}

int StackSize(Stack *s)

{

    return (int)(s->top-s->base);

}

int EmptyStack(Stack *s)

{

    if(s->base==s->top)

    {

        return 1;

    }

    else

        return 2;

}

void ClearStack(Stack *s)

{

    s->top=s->base;

}

void Push(Stack *s,Node d)

{

    

    if(s->top-s->base>=s->Capacity)

    {

        s->base=(Node *)realloc(s->base,AppSize*sizeof(Node));

        if(!s->base)

            return ;

        s->top=s->base+s->Capacity;

        s->Capacity=AppSize+InISize;

    }

    *(s->top)=d;

    s->top++;

}

bool searchIn(const int &j,Stack s)

{

    bool res=false;

    

    while(s.base!=s.top)

    {

        if(s.base->node==j)

        {

            res=true;

            break;

        }

        s.base++;

    }

    return res;

}

void DijkstraAlgorithm(Stack *s)

{

    int n;

    cout<<"the amount of node:";

    cin>>n;

    int **a=new int * [n+1];

    for(int i=0;i<=n;i++)

        a[i]=new int [n+1];

    cout<<"specific data:(as -1 is on behalf of ending)"<<endl;

    for(int i=1;i<n+1;i++)

    {

        for(int j=1;j<n+1;j++)

            a[i][j]=MaxLine;

    }

    int i,j,k;

    while(cin>>i>>j>>k)

    {

        if(i>n||j>n)

            break;

        if((i>0&&i<=n)&&(j>0&&j<=n))

        {

            a[i][j]=k;

        }

        if(i==-1||j==-1)

            break;

    }

    int start;

    cout<<"the starting node:";

    cin>>start;

    int temp=start;

    int value=MaxLine;

    Node *l=(Node*)malloc(sizeof(Node));

    l->node=start;

    l->value=value;

    Push(s, *l);

    for(int j=1;j<=n;j++)

    {

       if(a[temp][j]!=MaxLine-1)

        if(value>a[start][j])

        {

            value=a[start][j];

            temp=j;

        }

       

        //

    }

    Node *lose=(Node*)malloc(sizeof(Node));

    lose->node=temp;

    lose->value=value;

    Push(s, *lose);

    for(int i=1;i<=n;i++){

        if(a[temp][i]==MaxLine)

            cout<<"not ";

        else

            cout<<" "<<a[temp][i];

        

    }

    cout<<endl;

    while(StackSize(s)!=n){

        value=MaxLine;

    for(int i=1;i<=n;i++)

    {

        for(int j=1;j<=n;j++)

        {

            if(i==temp)

            {

                if(a[temp][j]!=MaxLine&&a[start][i]!=MaxLine)

                {

                    if(a[temp][j]+a[start][i]<a[start][j])

                    {

                        a[temp][j]=a[temp][j]+a[start][i];

                    }

                    else

                    {

                        a[temp][j]=a[start][j];

                    }

                }

                if(a[temp][j]==MaxLine&&a[start][i]!=MaxLine)

                {

                    a[temp][j]=a[start][j];

                }

            }

        }

    }

        for(int i=1;i<=n;i++){

            if(a[temp][i]==MaxLine)

                cout<<"not";

            else

            cout<<" "<<a[temp][i];

            

        }

        cout<<endl;

    start=temp;

    for(int j=1;j<=n;j++)

    {

        if(a[start][j]!=MaxLine&&!searchIn(j, *s))

            if(value>a[start][j])

            {

                value=a[start][j];

                temp=j;

            }

       

        //

    }

    Node *lose=(Node*)malloc(sizeof(Node));

    lose->node=temp;

    lose->value=value;

    Push(s, *lose);

    

    }

}

void showLink(Stack *s)

{

    Node *h=s->base;

    Node *p=s->base;

    while(h!=s->top)

    {

        cout<<" "<<h->node;

        h++;

    }

    cout<<endl;

    while(p!=s->top)

    {

        cout<<" "<<p->value;

        p++;

    }

    cout<<endl;

}



int main(int argc, const char * argv[]) {

    // insert code here...

    std::cout << "Hello, World!\n";

    Stack S;

    IniStack(&S);

    DijkstraAlgorithm(&S);

    cout<<StackSize(&S)<<endl;

    showLink(&S);

    return 0;

}
    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/qq_43051529/article/details/84110028
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞