# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
// This struct is used to represent other vertices that can be reached from a vertex.
Struct Enode
{
Int secpoint; // vertex number
Int weight;
Enode * Next; // pointer to the next Vertex
};
// This struct indicates the information of each vertex.
Struct pnode
{
Char value; // vertex Value
Int indegree;
Int outdegree;
Enode * Next; // pointer to the other first vertex that can be reached by the vertex
};
// Structure of the graph, which has a maximum of 100 vertices
Struct Map
{
Pnode point [100]; // The subscript of the array is the vertex number of the vertex.
Int numpoint, numedge;
};
// Graph creation Function
Struct map * createmap ()
{
Struct map * MP = (struct map *) malloc (sizeof (struct map ));
Int I, J;
Int firp, SECP, weight;
Int nump, nume;
Char INFOP;
Memset (MP, 0, sizeof (struct map ));
Printf ("Enter the number of vertices and edges in the format of 'number of vertices, edges ': \ n ");
Scanf ("% d, % d", & nump, & nume );
MP-> numpoint = nump;
MP-> numedge = nume;
Printf ("enter the information of each vertex. continuous input without separators: \ n ");
Fflush (stdin );
For (I = 0; I <MP-> numpoint; I ++)
{
Scanf ("% C", & INFOP );
MP-> point [I]. value = INFOP;
}
Printf ("Enter the edge and weight in the format of 'vertex-vertex, weighting '\ n ");
Fflush (stdin );
For (j = 0; j <MP-> numedge; j ++)
{
Scanf ("% d-% d, % d", & firp, & SECP, & weight );
Struct Enode * newnode = (struct Enode *) malloc (sizeof (struct Enode ));
MP-> point [firp]. outdegree ++;
MP-> point [SECP]. indegree ++;
Newnode-> secpoint = SECP;
Newnode-> Weight = weight;
Newnode-> next = MP-> point [firp]. Next;
MP-> point [firp]. Next = newnode;
}
Return MP;
}
// The dijela algorithm calculates the shortest path from a vertex to another vertex in the graph.
Void Dijkstra (struct map * MP, int sourcepoint)
{
Int minpath [20]; // record the shortest path value of each vertex
Bool known [20] = {0}; // the Shortest Path of the vertex is true after it is determined.
Int mintmp; // temporary variable used to select the minimum path length in the vertex of the uncertain Shortest Path
Int mine; // record the vertex in the previous step
Int I, knownflag = 1;
Struct Enode * TMP;
Known [sourcepoint] = true;
Memset (minpath, 1, sizeof (minpath ));
Minpath [sourcepoint] = 0;
// Set the path length of the vertex that can be reached from the Source Vertex to the weight of the edge from The Source Vertex To This vertex.
For (TMP = MP-> point [sourcepoint]. Next; TMP! = NULL; TMP = TMP-> next)
{
Minpath [TMP-> secpoint] = TMP-> weight;
}
// Loop if the Shortest Path of the vertex is not determined
While (knownflag <= MP-> numpoint)
{
Mintmp = 32767; // The maximum integer for the initial value
// Select the minimum path length in the vertex of the uncertain shortest path.
// Determine that the value is the shortest path to the vertex.
For (I = 0; I <MP-> numpoint; I ++)
{
If (minpath [I] <mintmp & known [I] = false)
{
Mintmp = minpath [I];
Mine = I;
}
}
// Set the bit and accumulate
Known [mine] = true;
Knownflag ++;
// Update the value of the current Shortest Path of the vertex that the new vertex can reach
For (TMP = MP-> point [mine]. Next; TMP! = NULL; TMP = TMP-> next)
{
// Add the Shortest Path of the new node to its own weight. If the weight is smaller than the current Shortest Path value, it is updated.
If (minpath [mine] + TMP-> weight <minpath [TMP-> secpoint])
{
Minpath [TMP-> secpoint] = minpath [mine] + TMP-> weight;
}
}
}
For (I = 0; I <MP-> numpoint; I ++)
{
Printf ("% d: % d \ n", I, minpath [I]);
}
}
Int main ()
{
Struct map * MP = createmap ();
Dijkstra (MP, 0 );
Return 1;
}