# 最短路径Dijkstra算法

#include<stdio.h>

#define Max 10
#define oo 99999

/*代码完全原创思路参考Dijkstra*/

typedef struct Graph
{
int arc[Max][Max];    //临街矩阵
int numVex;
};

int Again(int j , int Exit[])
{
for(int i = 0 ; Exit[i] != -1 ; i++)
{
if( Exit[i] == j )
return 0;
}
return 1;
}
void Dijkstra(Graph G)
{
int Min_weight[G.numVex],Exit[G.numVex]; //1，用于存某节点离零结点最小权值和 2，存已存在的结点  Exit[1] = 1 表示1结点已存在路径中
for(int i = 0; i < G.numVex; i++)
{
Min_weight[i] = G.arc[0][i];
Exit[i] = -1;
}
Exit[0] = 0;
while(Exit[G.numVex-1] == -1)
{
int min = oo;
int i,j,k;
for(k = 0 ; k < G.numVex&&Exit[k] != -1; k++) //寻找最小权值和过程
{
for(j = 1; j < G.numVex ; j++)
{
if(Min_weight[Exit[k]] + G.arc[Exit[k]][j] < min&&Again(j,Exit))  // Again函数用于判断某节点是否重复 //若重复在则不对该结点边权值进行
{
min = Min_weight[Exit[k]] + G.arc[Exit[k]][j];
i = j;
}
}
}
Min_weight[i] = min;
Exit[k] = i;
}
for(int i = 0 ; i < G.numVex ; i++)
{
printf("%d ",Min_weight[i]);
}
}

int main()

Graph graph;
graph.numVex = 9;
for(int i = 0 ; i < graph.numVex ; i++)
{
for(int j = 0 ; j < graph.numVex ; j++)
{
scanf("%d",&graph.arc[i][j]);
}
}
Dijkstra(graph);
return 0;
}