数据结构 图 最短路径
2007-04-10 22:29
225 查看
(1)从某个源点到其余各顶点的最短路径
Dijkstra的代码如下:
头文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define INFINITY 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX_VERTEX_NUM 20
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef int InfoType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef char VertexType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef int VRType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef enum ...{DG, DN, UDG, UDN} GraphKind;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef struct ArcCell...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
VRType adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InfoType *info;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef struct...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
VertexType vexs[MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AdjMatrix arcs;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int vexnum, arcnum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GraphKind kind;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}MGraph;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int weight[][6] = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{0, INFINITY, 10, INFINITY, 30, 100},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, 0, 5, INFINITY, INFINITY, INFINITY},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, 0, 50, INFINITY, INFINITY},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, INFINITY, 0, INFINITY, 10},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, INFINITY, 20, 0, 60},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, INFINITY, INFINITY, INFINITY, 0}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//#define INPUT 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
源文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "graph_dj.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdio.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdlib.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createDG(MGraph &g)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createDN(MGraph &g)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createUDG(MGraph &g)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int locate(MGraph g, char name)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(name == g.vexs[i])...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return -1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createUDN(MGraph &g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#ifdef INPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the number of vertex and arcs: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d %d", &g.vexnum, &g.arcnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the number of vertex: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d", &g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 0, j = 0, k = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the name of vertex: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%c", &g.vexs[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].adj = INFINITY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].info = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#ifdef INPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char v1, v2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int w;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the %d arcs v1 v2 and weight: ", g.arcnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(k = 0; k < g.arcnum; k++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%c %c %d", &v1, &v2, &w);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i = locate(g, v1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
j = locate(g, v2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].adj = w;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("now constructing the matrix of graph... ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].adj = weight[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void printGraph(MGraph g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d ", g.arcs[i][j].adj);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createGragh(MGraph &g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("please input the type of graph: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d", &g.kind);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
switch(g.kind)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DG:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createDG(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DN:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createDN(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case UDG:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createUDG(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case UDN:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createUDN(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printGraph(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void dijkstra(MGraph g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int *dist = (int *)malloc(sizeof(int) * g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int *parent = (int *)malloc(sizeof(int) * g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int *final = (int *)malloc(sizeof(int) * g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i, j, k, cost;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//init the arrays
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[i] = g.arcs[0][i].adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[i] = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final[i] = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//join the start 0.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final[0] = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 1; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//get the min of dist
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cost = INFINITY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k = -1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 1; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(dist[j] < cost && final[j] == 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cost = dist[j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k = j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(k > 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//join the k node
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final[k] = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//update the dist
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int t = 1; t < g.vexnum; t++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(final[t] == 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(dist[t] > dist[k] + g.arcs[k][t].adj)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[t] = dist[k] + g.arcs[k][t].adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[t] = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//print the dist and parent array
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("the shortest path from %c is as follows: ", g.vexs[0]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%c via %c's dist is %d ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.vexs[i], g.vexs[parent[i]], dist[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void main()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MGraph g;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createGragh(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dijkstra(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序的执行结果如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
please input the type of graph:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the number of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the name of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
e
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
now constructing the matrix of graph...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0 10000 10 10000 30 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 0 5 10000 10000 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 0 50 10000 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 10000 0 10000 10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 10000 20 0 60
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 10000 10000 10000 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
the shortest path from a is as follows:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a via a's dist is 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b via a's dist is 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c via a's dist is 10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d via e's dist is 50
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
e via a's dist is 30
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f via d's dist is 60
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Press any key to continue
说明:Dijkstra算法的复杂度是O(n * n)。
(2)每一对顶点之间的最短路径
方法一:对每个顶点依次运行Dijkstra,复杂度O(n*n*n)。
方法二:floyd,复杂度O(n*n*n)。
头文件修改:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int weight[][3] = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{0, 4, 11},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{6, 0, 2},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{3, INFINITY, 0}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
源文件添加:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void floyd(MGraph g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int dist[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int parent[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//init the arrays
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i, j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[i][j] = g.arcs[i][j].adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[i][j] = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//compute the shortest path
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int k = 0; k < g.vexnum; k++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(i != j && i != k && j != k)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(dist[i][j] > dist[i][k] + dist[k][j])...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[i][j] = dist[i][k] + dist[k][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[i][j] = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//print the shortest path and parent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d(%c) ", dist[i][j], g.vexs[parent[i][j]]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void main()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MGraph g;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createGragh(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dijkstra(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
floyd(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序的运行结果如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
please input the type of graph:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the number of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the name of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
now constructing the matrix of graph...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0 4 11
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
6 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3 10000 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
the shortest path from a is as follows:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a via a's dist is 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b via a's dist is 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c via b's dist is 6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0(a) 4(a) 6(b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
5(c) 0(b) 2(b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3(c) 7(a) 0(c)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Press any key to continue
Dijkstra的代码如下:
头文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define INFINITY 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX_VERTEX_NUM 20
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef int InfoType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef char VertexType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef int VRType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef enum ...{DG, DN, UDG, UDN} GraphKind;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef struct ArcCell...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
VRType adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InfoType *info;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
typedef struct...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
VertexType vexs[MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AdjMatrix arcs;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int vexnum, arcnum;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GraphKind kind;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}MGraph;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int weight[][6] = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{0, INFINITY, 10, INFINITY, 30, 100},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, 0, 5, INFINITY, INFINITY, INFINITY},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, 0, 50, INFINITY, INFINITY},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, INFINITY, 0, INFINITY, 10},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, INFINITY, 20, 0, 60},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{INFINITY, INFINITY, INFINITY, INFINITY, INFINITY, 0}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//#define INPUT 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
源文件:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "graph_dj.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdio.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdlib.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createDG(MGraph &g)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createDN(MGraph &g)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createUDG(MGraph &g)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int locate(MGraph g, char name)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(name == g.vexs[i])...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return -1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createUDN(MGraph &g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#ifdef INPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the number of vertex and arcs: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d %d", &g.vexnum, &g.arcnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the number of vertex: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d", &g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 0, j = 0, k = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the name of vertex: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%c", &g.vexs[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].adj = INFINITY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].info = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#ifdef INPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char v1, v2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int w;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("input the %d arcs v1 v2 and weight: ", g.arcnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(k = 0; k < g.arcnum; k++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%c %c %d", &v1, &v2, &w);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fflush(stdin);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i = locate(g, v1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
j = locate(g, v2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].adj = w;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("now constructing the matrix of graph... ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.arcs[i][j].adj = weight[i][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void printGraph(MGraph g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d ", g.arcs[i][j].adj);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void createGragh(MGraph &g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("please input the type of graph: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d", &g.kind);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
switch(g.kind)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DG:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createDG(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DN:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createDN(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case UDG:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createUDG(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case UDN:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createUDN(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printGraph(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void dijkstra(MGraph g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int *dist = (int *)malloc(sizeof(int) * g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int *parent = (int *)malloc(sizeof(int) * g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int *final = (int *)malloc(sizeof(int) * g.vexnum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i, j, k, cost;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//init the arrays
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[i] = g.arcs[0][i].adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[i] = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final[i] = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//join the start 0.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final[0] = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 1; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//get the min of dist
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cost = INFINITY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k = -1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 1; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(dist[j] < cost && final[j] == 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cost = dist[j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
k = j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(k > 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//join the k node
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final[k] = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//update the dist
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int t = 1; t < g.vexnum; t++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(final[t] == 0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(dist[t] > dist[k] + g.arcs[k][t].adj)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[t] = dist[k] + g.arcs[k][t].adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[t] = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//print the dist and parent array
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("the shortest path from %c is as follows: ", g.vexs[0]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%c via %c's dist is %d ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.vexs[i], g.vexs[parent[i]], dist[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void main()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MGraph g;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createGragh(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dijkstra(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序的执行结果如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
please input the type of graph:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the number of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the name of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
e
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
now constructing the matrix of graph...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0 10000 10 10000 30 100
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 0 5 10000 10000 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 0 50 10000 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 10000 0 10000 10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 10000 20 0 60
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
10000 10000 10000 10000 10000 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
the shortest path from a is as follows:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a via a's dist is 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b via a's dist is 10000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c via a's dist is 10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
d via e's dist is 50
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
e via a's dist is 30
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
f via d's dist is 60
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Press any key to continue
说明:Dijkstra算法的复杂度是O(n * n)。
(2)每一对顶点之间的最短路径
方法一:对每个顶点依次运行Dijkstra,复杂度O(n*n*n)。
方法二:floyd,复杂度O(n*n*n)。
头文件修改:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int weight[][3] = ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{0, 4, 11},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{6, 0, 2},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{3, INFINITY, 0}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
源文件添加:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void floyd(MGraph g)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int dist[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int parent[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//init the arrays
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i, j;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[i][j] = g.arcs[i][j].adj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[i][j] = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//compute the shortest path
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int k = 0; k < g.vexnum; k++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(i != j && i != k && j != k)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(dist[i][j] > dist[i][k] + dist[k][j])...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dist[i][j] = dist[i][k] + dist[k][j];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent[i][j] = k;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//print the shortest path and parent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(i = 0; i < g.vexnum; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(j = 0; j < g.vexnum; j++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d(%c) ", dist[i][j], g.vexs[parent[i][j]]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void main()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MGraph g;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
createGragh(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dijkstra(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
floyd(g);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序的运行结果如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
please input the type of graph:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the number of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
input the name of vertex:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
now constructing the matrix of graph...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0 4 11
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
6 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3 10000 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
the shortest path from a is as follows:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a via a's dist is 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b via a's dist is 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
c via b's dist is 6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0(a) 4(a) 6(b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
5(c) 0(b) 2(b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3(c) 7(a) 0(c)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Press any key to continue
相关文章推荐
- 数据结构 18 图 最短路径-迪杰斯特拉最短算法
- [数据结构]--图(图的遍历,最小生成树,最短路径算法)
- 数据结构之最短路径的 Dijkstra Algorithm && Floyd Algorithm
- 【数据结构】最短路径——Dijkstra算法
- 数据结构之最短路径(DijKstra)
- [数据结构]第六次作业:图的建立、遍历、最小生成树、最短路径
- 数据结构 JAVA描述(八) 最短路径+拓扑排序+关键路径
- C数据结构(文件操作,随机数,排序,栈和队列,图和遍历,最小生成树,最短路径)程序例子
- 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径
- 最短路径---Dijkstra迪杰特斯拉算法---《数据结构》严蔚敏
- 数据结构 学习笔记(八):图(中):最短路径问题(单源最短路径 Dijkstra,多源最短路径 Floyd)
- 数据结构之 最短路径2
- 【数据结构】 图的最短路径——dijkstra的一个计算实例
- 数据结构::迷宫(二)--栈的一个应用(求迷宫最短路径)
- 数据结构学习记录-迷宫设计(最短路径)
- [数据结构]Dijkstra算法求单源最短路径
- 数据结构:最短路径算法之Bellman-Ford算法
- 【数据结构】最短路径_Dijkstra
- C语言基本数据结构之三(图的广度及深度遍历,求单源最短路径的Dijkstra算法)
- 数据结构之 最短路径2