提交 797bc265 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 b83e6652
#ifndef SHORTESTPATH_H_
#define SHORTESTPATH_H_
//
// Include the C standard library header file here
//
#include <stdio.h>
//
// Other header files are included here
//
//
// Define the data structure here
//
#define MAX 100000000 // Denotes the direct connection between two vertices, denoted by MAX in the adjacency matrix
// Denotes the direct connection between two vertices, denoted by MAX in the adjacency matrix
#define MAX 100000000
#define VERTEXNUM 6 // Vertex number
typedef struct GraphMatrix{
typedef struct GraphMatrix
{
int Arcs[VERTEXNUM][VERTEXNUM]; // Edge information
}GraphMatrix;
typedef struct ShortestPath{
typedef struct ShortestPath
{
int length; // Shortest path length
int prevex; // The leading vertex of Vi on the shortest path from Vo to Vi
}ShortestPath;
//
// Declare functions
//
void Dijkstra(GraphMatrix* pGraph);
void OutputResult(GraphMatrix* pGraph);
//
// Declare global variables here
//
extern ShortestPath dist[MAX];
#endif /* SHORTESTPATH_H_ */
......@@ -8,14 +8,17 @@ int main(int argc, char* argv[])
//
// Initialization graph (adjacency matrix representation of graph)
//
GraphMatrix Graph = {{
{ 0, 50, 10, MAX, 45, MAX},
{ MAX, 0, 15, MAX, 5, MAX},
{ 20, 100, 0, 15, MAX, MAX},
{ MAX, 20, MAX, 0, 35, MAX},
{ MAX, MAX, MAX, 30, 0, MAX},
{ MAX, MAX, MAX, 3, MAX, 0}
}};
GraphMatrix Graph =
{
{
{ 0, 50, 10, MAX, 45, MAX},
{ MAX, 0, 15, MAX, 5, MAX},
{ 20, 100, 0, 15, MAX, MAX},
{ MAX, 20, MAX, 0, 35, MAX},
{ MAX, MAX, MAX, 30, 0, MAX},
{ MAX, MAX, MAX, 3, MAX, 0}
}
};
//
//Shortest path (Dijkstra algorithm)
......@@ -60,15 +63,15 @@ void OutputResult(GraphMatrix* pGraph)
int i;
printf("ShortestPath: ");
for(i=0; i < VERTEXNUM; i++)
for (i=0; i < VERTEXNUM; i++)
{
if(pGraph->Arcs[i][i] == 1)
if (pGraph->Arcs[i][i] == 1)
{
printf("[%3d, %2d]", dist[i].length, dist[i].prevex);
}
else
{
if(dist[i].length == MAX)
if (dist[i].length == MAX)
{
printf("(%3s, %2d)", "MAX", dist[i].prevex);
}
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论