void Dijkstra(MatrixGraph G, int v0, int distance[], int path[]) { int n = G.Vertices.size; int* s = (int*)malloc(sizeof(int) * n); int minDis, i, j, u; for (i = 0; i < n; i++) { distance[i] = G.edge[v0][i]; s[i] = 0; if (i != v0 && distance[i], MaxWeight) path[i] = v0; else path[i] = -1; } s[v0] = 1; for (i = 1; i < n; i++) { minDis = MaxWeight; for (j = 0; j < n; j++) { if (s[j] == 0 && distance[j] < minDis) { u = j; minDis = distance[j]; } } if (minDis == MaxWeight) return; s[u] = 1; for (j = 0; j < n; j++) { if (s[j] == 0 && G.edge[u][j] < MaxWeight && distance[u] + G.edge[u][j] < distance[j]) { distance[j] = distance[u] + G.edge[u][j]; path[j] = u; } } } }有关这段代码的主函数怎么用c语音写
时间: 2024-01-20 18:02:31 浏览: 63
gui.rar_gui java_java Dijkstra g_java gui
以下是一个示例的主函数,实现了从文件中读取图的信息并调用 Dijkstra 函数计算最短路径信息:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxWeight 0x3f3f3f3f
typedef struct
{
int** edge; // 存储图的边权重
int* Vertices; // 存储图的各个顶点编号
int n; // 图中顶点数
int e; // 图中边数
} MatrixGraph;
void Dijkstra(MatrixGraph G, int v0, int distance[], int path[]);
int main()
{
MatrixGraph G;
FILE* fp;
char filename[50];
printf("请输入包含图信息的文件名:");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("文件打开失败!\n");
return 0;
}
fscanf(fp, "%d%d", &(G.n), &(G.e));
G.Vertices = (int*)malloc(sizeof(int) * G.n);
G.edge = (int**)malloc(sizeof(int*) * G.n);
for (int i = 0; i < G.n; i++)
{
G.edge[i] = (int*)malloc(sizeof(int) * G.n);
for (int j = 0; j < G.n; j++)
{
if (i == j)
{
G.edge[i][j] = 0;
}
else
{
G.edge[i][j] = MaxWeight;
}
}
}
for (int i = 0; i < G.n; i++)
{
fscanf(fp, "%d", &(G.Vertices[i]));
}
for (int i = 0; i < G.e; i++)
{
int u, v, w;
fscanf(fp, "%d%d%d", &u, &v, &w);
G.edge[u][v] = w;
}
fclose(fp);
int v0 = 0; // 起点编号
int n = G.n; // 顶点数
int* distance = (int*)malloc(sizeof(int) * n);
int* path = (int*)malloc(sizeof(int) * n);
Dijkstra(G, v0, distance, path);
printf("从起点 %d 到各个顶点的最短距离为:\n", v0);
for (int i = 0; i < n; i++)
{
printf("%d ", distance[i]);
}
printf("\n");
printf("从起点 %d 到各个顶点的最短路径为:\n", v0);
for (int i = 0; i < n; i++)
{
if (path[i] == -1)
{
printf("不存在最短路径\n");
}
else
{
printf("%d <- ", i);
int j = path[i];
while (j != v0)
{
printf("%d <- ", j);
j = path[j];
}
printf("%d\n", v0);
}
}
free(distance);
free(path);
for (int i = 0; i < G.n; i++)
{
free(G.edge[i]);
}
free(G.edge);
free(G.Vertices);
return 0;
}
void Dijkstra(MatrixGraph G, int v0, int distance[], int path[])
{
int n = G.n;
int* s = (int*)malloc(sizeof(int) * n);
int minDis, i, j, u;
for (i = 0; i < n; i++)
{
distance[i] = G.edge[v0][i];
s[i] = 0;
if (i != v0 && distance[i] < MaxWeight)
path[i] = v0;
else
path[i] = -1;
}
s[v0] = 1;
for (i = 1; i < n; i++)
{
minDis = MaxWeight;
for (j = 0; j < n; j++)
{
if (s[j] == 0 && distance[j] < minDis)
{
u = j;
minDis = distance[j];
}
}
if (minDis == MaxWeight)
return;
s[u] = 1;
for (j = 0; j < n; j++)
{
if (s[j] == 0 && G.edge[u][j] < MaxWeight && distance[u] + G.edge[u][j] < distance[j])
{
distance[j] = distance[u] + G.edge[u][j];
path[j] = u;
}
}
}
free(s);
}
```
阅读全文