This commit is contained in:
2025-12-16 09:06:45 +08:00
parent e325fd405e
commit 0c012f7938
+21 -13
View File
@@ -14,24 +14,24 @@ typedef struct
typedef AMGraphStruct *AMGraph;
// 定义最大顶点数和最大边权值
#define MaxInt 99999 // 最大边权值,用于初始化距离数组
#define MAXN 1005 // 最大顶点数
static bool S[MAXN]; // S[i]为true表示顶点i已加入集合S,否则未加入
static int D[MAXN]; // D[i]为v0到顶点i的当前最短路径长度
static int Path[MAXN]; // Path[i]为v0到顶点i的最短路径上的前驱顶点
#define MaxInt 99999 // 最大边权值,用于初始化距离数组
#define MAXN 1005 // 最大顶点数
static bool S[MAXN]; // S[i]为true表示顶点i已加入集合S,否则未加入
static int D[MAXN]; // D[i]为v0到顶点i的当前最短路径长度
static int Path[MAXN]; // Path[i]为v0到顶点i的最短路径上的前驱顶点
// Dijkstra算法求最短路径
void ShortestPath_DIJ(AMGraph G, int v0)
{
// 初始化
int n, i, v, w; // n为顶点数,i为循环变量,v为当前顶点,w为邻接顶点
int n, i, v, w; // n为顶点数,i为循环变量,v为当前顶点,w为邻接顶点
int min;
n = G->vexnum;
for (v = 0; v < n;++v)
for (v = 0; v < n; ++v)
{
S[v] = false;
D[v] = G->arcs[v0][v];
if (D[v]<MaxInt)
if (D[v] < MaxInt)
{
Path[v] = v0;
}
@@ -50,7 +50,7 @@ void ShortestPath_DIJ(AMGraph G, int v0)
min = MaxInt;
for (w = 0; w < n; ++w)
{
if (!S[w] && D[w]<min)
if (!S[w] && D[w] < min)
{
v = w;
min = D[w];
@@ -62,7 +62,7 @@ void ShortestPath_DIJ(AMGraph G, int v0)
for (w = 0; w < n; ++w)
{
if (!S[w] && (D[v] + G->arcs[v][w] < D[w]))
{
{
D[w] = D[v] + G->arcs[v][w];
Path[w] = v;
}
@@ -79,6 +79,7 @@ int main(void)
#endif
// 输入顶点数和边数
printf("请输入顶点数n和边数m");
int n, m;
if (scanf("%d %d", &n, &m) != 2)
{
@@ -93,19 +94,26 @@ int main(void)
{
g->arcs[i] = (int *)malloc((size_t)n * sizeof(int));
for (int j = 0; j < n; ++j)
{
g->arcs[i][j] = (i == j) ? 0 : MaxInt;
}
}
// 输入边信息
for (int i = 0; i < m; ++i)
{
printf("请输入第%d条边的顶点x、y和边权z", i + 1);
int x, y, z;
if (scanf("%d %d %d", &x, &y, &z) != 3)
{
return 0;
}
if (x >= 1 && x <= n && y >= 1 && y <= n)
{
if (z < g->arcs[x - 1][y - 1])
{
g->arcs[x - 1][y - 1] = z;
}
}
}
@@ -113,11 +121,11 @@ int main(void)
ShortestPath_DIJ(g, 0);
if (D[n - 1] >= MaxInt)
{
printf("-1\n");
printf("从顶点0到顶点%d不存在路径\n", n - 1);
}
else
{
printf("%d\n", D[n - 1]);
printf("从顶点0到顶点%d的最短路径长度为:%d\n", n - 1, D[n - 1]);
}
// 释放内存
@@ -127,6 +135,6 @@ int main(void)
}
free(g->arcs);
free(g);
return 0;
}