I already wrote the corresponding function in the last code, so I’ll post it again.
The title
code
#include <stdio.h>
#include <stdlib.h>
struct graphList
{
int vexNum;
int graph[120] [120];
};
int P[120] [120];P (1,3) = 2 indicates that the minimum path from vertex 1 to vertex 3 goes through 2
void createNewGraphList(struct graphList *gList)
{
int i,j;
scanf ("%d", &(gList -> vexNum));
for (i=0; i < gList->vexNum; i++) {for (j = 0; j < gList -> vexNum; j++)
{
scanf ("%d", &(gList -> graph[i][j])); }}}void floydAlgorithm(struct graphList *gList)
{
int v,w,k;
// Initializes the minimum path matrix of the Floyd algorithm
for(v = 0; v < gList->vexNum; v++){
for(w = 0; w < gList->vexNum; w++){ P[v][w] = w; }}// This is the core of Freud's algorithm
//k is the intermediate point
for(k=0; k<gList->vexNum; k++) {/ / v as a starting point
for(v=0; v<gList->vexNum; v++) {/ / w to the end
for(w=0; w<gList->vexNum; w++) {if(gList->graph[v][w]>gList->graph[v][k]+gList->graph[k][w])
{
gList->graph[v][w]=gList->graph[v][k]+gList->graph[k][w];// Update the minimum path
P[v][w] = P[v][k];// Update the middle vertex of the minimum path
}
}
}
}
}
void putOutMinStepNum(struct graphList *gList)
{
int n;
int a[10];
int i, j,k=0;
scanf ("%d", &n);// The number of outputs
while (n--)
{
scanf ("%d%d", &i, &j);
a[k]=gList -> graph[i][j];
k++;
}
for(i=0; i<k; i++)printf("%d\n",a[i]);
}
void putOutMinStep(a)
{
int i,j;
int n;
int a[100] [100];
scanf("%d",&n);
for(int t=0; t<n; t++) {scanf("%d %d",&a[t][0],&a[t][1]);
}
for(int t=0; t<n; t++) {int k=P[a[t][0]][a[t][1]].printf("%d\n", a[t][0]);// Print the starting point
while(k! =a[t][1])
{
printf("%d\n", k);// Prints the middle point
k = P[k][a[t][1]];
}
printf("%d\n",k);// Prints the middle point}}int main(a)
{
struct graphList gList;
createNewGraphList(&gList);
floydAlgorithm (&gList);
//putOutMinStepNum (&gList);
putOutMinStep();
return 0;
}
Copy the code
As shown in figure: