邻接矩阵 Dijkstra 算法

版权声明:欢迎大佬批评指正!O(∩_∩)O https://blog.csdn.net/wyh1618/article/details/82971549
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<malloc.h>
#define INF 99999
#define MAXV 100
using namespace std;

typedef struct
{
    int no;
}VertexType;

typedef struct
{
    int edges[100][100];
    int n,e;
    VertexType vexs[100];
}MatGraph;


void CreatMat(MatGraph &g,int A[6][10],int n,int e)
{
    int i,j;
    g.n=n;
    g.e=e;
    for(i=0;i<g.n;i++)
    {
        for(j=0;j<g.n;j++)
        {
            g.edges[i][j]=A[i][j];
        }
    }
}

void DispMat(MatGraph g)
{
    int i,j;
    for(i=0;i<g.n;i++)
    {
        for(j=0;j<g.n;j++)
        {
            if(g.edges[i][j]!=INF)
            {
                printf("%4d",g.edges[i][j]);
            }
            else
            {
                printf("%4s","#");
            }
        }
        cout<<endl;
    }
}

void Dispath(MatGraph g,int dist[],int path[],int S[],int v)
{
    int i,j,k;
    int apath[100];
    int d;
    for(i=0;i<g.n;i++)
    {
        if(S[i]==1&&i!=v)
        {
            printf("From %d to %d DistanceLength: %d\n",v,i,dist[i]);
            d=0;
            apath[d]=i;
            k=path[i];
            if(k==-1)
            {
                cout<<"no distance"<<endl;
            }
            else
            {
                while(k!=v)
                {
                    d++;
                    apath[d]=k;
                    k=path[k];
                }
                d++;
                apath[d]=v;
                cout<<"Distance"<<" "<<v<<" ";
                for(j=d-1;j>=0;j--)
                {
                    cout<<" "<<apath[j]<<" ";
                }
                cout<<"\n"<<endl;
            }
        }

    }
}

void Dijkstra(MatGraph g,int v)
{
    int dist[100];
    int path[100];
    int S[100];
    int Mindis ,i,j,k,u;
    for(i=0;i<g.n;i++)
    {
        dist[i]=g.edges[v][i];
        S[i]=0;
        if(g.edges[v][i]<INF)
        {
            path[i]=v;
        }
        else
        {
            path[i]=-1;
        }
    }
    S[v]=1;
    path[v]=0;
    for(i=0;i<g.n-1;i++)
    {
        Mindis=INF;
        for(j=0;j<g.n;j++)
        {
            if(S[j]==0&&dist[j]<Mindis)
            {
                u=j;
                Mindis=dist[j];
            }
        }
        S[u]=1;
        for(j=0;j<g.n;j++)
        {
            if(S[j]==0)
            {
                if(g.edges[u][j]<INF&&dist[u]+g.edges[u][j]<dist[j])
                {
                    dist[j]=dist[u]+g.edges[u][j];
                    path[j]=u;
                }
            }
        }
    }
    Dispath(g,dist,path,S,v);
}

int main()
{
    MatGraph g;
    int A[6][10]={
    {0,5,INF,7,INF,INF}, {INF,0,4,INF,INF,INF},
    {8,INF,0,INF,INF,9}, {INF,INF,5,0,INF,6},
    {INF,INF,INF,5,0,INF}, {3,INF,INF,INF,1,0}};
    CreatMat(g,A,6,10);
    DispMat(g);
    Dijkstra(g,0);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wyh1618/article/details/82971549