{
for(w=v+1;w〈G.numVertexes;w++)
{
printf(”v%d-v%dweight:
%d",v,w,D[v][w]);
k=P[v][w];/*获得第一个路径顶点下标*/
printf(”path:
%d”,v);/*打印源点*/
while(k!
=w)/*如果路径顶点下标不是终点*/
{
printf("—>%d",k);/*打印路径顶点*/
k=P[k][w];/*获得下一个路径顶点下标*/
}
printf("-〉%d\n”,w);/*打印终点*/
}
printf(”\n”);
}
printf("最短路径D\n");
for(v=0;v〈G。
numVertexes;++v)
{
for(w=0;w〈G。
numVertexes;++w)
{
printf("%d\t”,D[v][w]);
}
printf("\n”);
}
printf(”最短路径P\n”);
for(v=0;v〈G.numVertexes;++v)
{
for(w=0;w〈G。
numVertexes;++w)
{
printf("%d",P[v][w]);
}
printf("\n");
}
return0;
}
/******************关键路径*******************/
#include"stdio.h"
#include"stdlib.h"
#include”io。
h”
#include"math。
h"
#include”time.h”
#defineOK1
#defineERROR0
#defineTRUE1
#defineFALSE0
#defineMAXEDGE30
#defineMAXVEX30
#defineINFINITY65535
typedefintStatus;/*Status是函数的类型,其值是函数结果状态代码,如OK等*/
int*etv,*ltv;/*事件最早发生时间和最迟发生时间数组,全局变量*/
int*stack2;/*用于存储拓扑序列的栈*/
inttop2;/*用于stack2的指针*/
/*邻接矩阵结构*/
typedefstruct
{
intvexs[MAXVEX];
intarc[MAXVEX][MAXVEX];
intnumVertexes,numEdges;
}MGraph;
/*邻接表结构*******************/
typedefstructEdgeNode/*边表结点*/
{
intadjvex;/*邻接点域,存储该顶点对应的下标*/
intweight;/*用于存储权值,对于非网图可以不需要*/
structEdgeNode*next;/*链域,指向下一个邻接点*/
}EdgeNode;
typedefstructVertexNode/*顶点表结点*/
{
intin;/*顶点入度*/
intdata;/*顶点域,存储顶点信息*/
EdgeNode*firstedge;/*边表头指针*/
}VertexNode,AdjList[MAXVEX];
typedefstruct
{
AdjListadjList;
intnumVertexes,numEdges;/*图中当前顶点数和边数*/
}graphAdjList,*GraphAdjList;
/******************************/
voidCreateMGraph(MGraph*G)/*构建图*/
{
inti,j;
/*printf(”请输入边数和顶点数:
");*/
G-〉numEdges=11;
G-〉numVertexes=8;
for(i=0;i〈G->numVertexes;i++)/*初始化图*/
{
G->vexs[i]=i;
}
for(i=0;i〈G-〉numVertexes;i++)/*初始化图*/
{
for(j=0;jnumVertexes;j++)
{
if(i==j)
G-〉arc[i][j]=0;
else
G-〉arc[i][j]=INFINITY;
}
}
G—>arc[0][1]=6;G—〉arc[0][2]=4;G—〉arc[0][3]=5;
G—〉arc[1][4]=1;G—>arc[2][4]=1;G—>arc[3][5]=2;
G-〉arc[4][6]=9;G—>arc[4][7]=7;G-〉arc[5][7]=4;
G—>arc[6][8]=2;G->arc[7][8]=4;
}
/*利用邻接矩阵构建邻接表*/
voidCreateALGraph(MGraphG,GraphAdjList*GL)
{
inti,j;
EdgeNode*e;
*GL=(GraphAdjList)malloc(sizeof(graphAdjList));
(*GL)->numVertexes=G.numVertexes;
(*GL)->numEdges=G。
numEdges;
for(i=0;i〈G。
numVertexes;i++)/*读入顶点信息,建立顶点表*/
{
(*GL)—〉adjList[i]。
in=0;
(*GL)—〉adjList[i]。
data=G.vexs[i];
(*GL)->adjList[i]。
firstedge=NULL;/*将边表置为空表*/
}
for(i=0;inumVertexes;i++)/*建立边表*/
{
for(j=0;j〈G.numVertexes;j++)
{
if(G.arc[i][j]!
=0&&G.arc[i][j]〈INFINITY)
{
e=(EdgeNode*)malloc(sizeof(EdgeNode));
e—〉adjvex=j;/*邻接序号为j*/
e—〉weight=G。
arc[i][j];
e->next=(*GL)->adjList[i].firstedge;/*将当前顶点上的指向的结点指针赋值给e*/
(*GL)->adjList[i]。
firstedge=e;/*将当前顶点的指针指向e*/
(*GL)—〉adjList[j].in++;
}
}
}
}
/*拓扑排序*/
StatusTopologicalSort(GraphAdjListGL)
{/*若GL无回路,则输出拓扑排序序列并返回1,若有回路返回0。
*/
EdgeNode*e;
inti,k,gettop;
inttop=0;/*用于栈指针下标*/
intcount=0;/*用于统计输出顶点的个数*/
int*stack;/*建栈将入度为0的顶点入栈*/
stack=(int*)malloc(GL->numVertexes*sizeof(int));
for(i=0;inumVertexes;i++)
if(0==GL—>adjList[i].in)/*将入度为0的顶点入栈*/
stack[++top]=i;top2=0;
etv=(int*)malloc(GL-〉numVertexes*sizeof(int));/*事件最早发生时间数组*/
for(i=0;i〈GL->numVertexes;i++)
etv[i]=0;/*初始化*/
stack2=(int*)malloc(GL-〉numVertexes*sizeof(int));/*初始化拓扑序列栈*/
printf(”TopologicalSort:
\t");
while(top!
=0)
{
gettop=stack[top——];
printf("%d—〉”,GL—>adjList[gettop].data);
count++;/*输出i号顶点,并计数*/
stack2[++top2]=gettop;/*将弹出的顶点序号压入拓扑序列的栈*/
for(e=GL—〉adjList[gettop]。
firstedge;e;e=e—〉next)
{
k=e—>adjvex;
if(!
(——GL-〉adjList[k].in))/*将i号顶点的邻接点的入度减1,如果减1后为0,则入栈*/
stack[++top]=k;
if((etv[gettop]+e->weight)>etv[k])/*求各顶点事件的最早发生时间etv值*/
etv[k]=etv[gettop]+