c++实现迪杰斯特拉求最短路径

#include <stdio.h>

#include <malloc.h>

#define INF 32767

#define  MAXV 100

typedef char InfoType;

typedef struct

{     int no;

       InfoType info;

} VertexType;

typedef struct

{     int edges[MAXV][MAXV];

       int n,e;

       VertexType vexs[MAXV];

} MatGraph;

typedef struct ANode

{     int adjvex;

       struct ANode *nextarc;

       int weight;

} ArcNode;

typedef struct Vnode

{     InfoType info;

       int count;

       ArcNode *firstarc;

} VNode;

typedef struct

{     VNode adjlist[MAXV];

       int n,e;

} AdjGraph;

void CreateMat(MatGraph &g,int A[MAXV][MAXV],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","∞");

              printf("\n");

       }

}

void CreateAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e)

{

       int i,j;

       ArcNode *p;

       G=(AdjGraph *)malloc(sizeof(AdjGraph));

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

              G->adjlist[i].firstarc=NULL;

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

              for (j=n-1;j>=0;j--)

                     if (A[i][j]!=0 && A[i][j]!=INF)

                     {     p=(ArcNode *)malloc(sizeof(ArcNode));

                            p->adjvex=j;

                            p->weight=A[i][j];

                            p->nextarc=G->adjlist[i].firstarc;

                            G->adjlist[i].firstarc=p;

                     }

       G->n=n; G->e=n;

}

void DispAdj(AdjGraph *G)

{

       ArcNode *p;

       for (int i=0;i<G->n;i++)

       {

              p=G->adjlist[i].firstarc;

              printf("%3d: ",i);

              while (p!=NULL)

              {

                     printf("%3d[%d]→",p->adjvex,p->weight);

                     p=p->nextarc;

              }

              printf("∧\n");

       }

}

void DestroyAdj(AdjGraph *&G)

{

       ArcNode *pre,*p;

       for (int i=0;i<G->n;i++)

       {     pre=G->adjlist[i].firstarc;

              if (pre!=NULL)

              {     p=pre->nextarc;

                     while (p!=NULL)

                     {     free(pre);

                            pre=p; p=p->nextarc;

                     }

                     free(pre);

              }

       }

       free(G);

}

void Dispath(MatGraph g,int dist[],int path[],int S[],int v)

{     int i,j,k;

       int apath[MAXV],d;

       for (i=0;i<g.n;i++)

              if (S[i]==1 && i!=v)

              {     printf("  从顶点%d到顶点%d的路径长度为:%d\t路径为:",v,i,dist[i]);

                     d=0; apath[d]=i;

                     k=path[i];

                     if (k==-1)

                            printf("无路径\n");

                     else

                     {     while (k!=v)

                            {     d++; apath[d]=k;

                                   k=path[k];

                            }

                            d++; apath[d]=v;

                            printf("%d",apath[d]);

                            for (j=d-1;j>=0;j--)

                                   printf(",%d",apath[j]);

                            printf("\n");

                     }

              }

}

void Dijkstra(MatGraph g,int v)

{     int dist[MAXV],path[MAXV];

       int S[MAXV];

       int Mindis,i,j,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()

{

       int v=0;

       MatGraph g;

       int A[MAXV][MAXV]={

              {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}};

       int n=6, e=10;

       CreateMat(g,A,n,e);

       printf("有向图G的邻接矩阵:\n"); DispMat(g);

       printf("狄克斯特拉算法求解结果:\n");

       Dijkstra(g,v);

       return 1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
迪杰斯特拉算法是一种用于最短路径的经典算法。它可以计算一个节点到其他所有节点的最短路径。下面是一个使用C++实现迪杰斯特拉算法最短路径的示例[^1]: ```cpp #include <iostream> #include <limits.h> #define V 9 int minDistance(int dist[], bool sptSet[]) { int min = INT_MAX, min_index; for (int v = 0; v < V; v++) { if (sptSet[v] == false && dist[v] <= min) { min = dist[v]; min_index = v; } } return min_index; } void printSolution(int dist[]) { std::cout << "Vertex \t\t Distance from Source" << std::endl; for (int i = 0; i < V; i++) { std::cout << i << " \t\t " << dist[i] << std::endl; } } void dijkstra(int graph[V][V], int src) { int dist[V]; bool sptSet[V]; for (int i = 0; i < V; i++) { dist[i] = INT_MAX; sptSet[i] = false; } dist[src] = 0; for (int count = 0; count < V - 1; count++) { int u = minDistance(dist, sptSet); sptSet[u] = true; for (int v = 0; v < V; v++) { if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; } } } printSolution(dist); } int main() { int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0}}; dijkstra(graph, 0); return 0; } ``` 这段使用邻接矩阵表示图,其中`V`表示节点的数量。通过调用`dijkstra`函数,可以计算从源节点到其他所有节点的最短路径,并将结果打印出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值