1、有向无环图的关键路径.txt51 自信是永不枯竭的源泉,自信是奔腾不息的波涛,自信是急流奋进的渠道,自信是真正的成功之母。#include#include#includeusing namespace std;#define MAX_VERTEX_NUM 20typedef struct ArcNodeint adjvex; /该弧所指向的顶点的位置struct ArcNode *nextarc; /指向下一条弧的指针int info; /弧上的信息/ string info; /该弧相关信息ArcNode;typedef struct VNodeint data; /顶点信息ArcNode
2、*firstarc; /指向第一条依附该顶点的弧的指针VNode,AdjListMAX_VERTEX_NUM;typedef structAdjList vertices; /存储图int venum,arcnum; /图的当前顶点数和弧数int kind; /图的种类标志ALGraph;int InDegreeMAX_VERTEX_NUM=0; /用于拓扑排序int CreateUG(ALGraphint i;for( i=0; iv1v2;ArcNode* current=G.verticesv1-1.firstarc;ArcNode* p=G.verticesv1-1.firstarc;
3、if(current=NULL)G.verticesv1-1.firstarc=new ArcNode;G.verticesv1-1.firstarc-adjvex=v2-1;G.verticesv1-1.firstarc-nextarc=NULL;elsewhile(current!=NULL)p=current;current=current-nextarc;current=new ArcNode;current-adjvex=v2-1;current-nextarc=NULL;p-nextarc=current;current=G.verticesv2-1.firstarc;p=G.ve
4、rticesv2-1.firstarc;if(current=NULL)G.verticesv2-1.firstarc=new ArcNode;G.verticesv2-1.firstarc-adjvex=v1-1;G.verticesv2-1.firstarc-nextarc=NULL;elsewhile(current!=NULL)p=current;current=current-nextarc;current=new ArcNode;current-adjvex=v1-1;current-nextarc=NULL;p-nextarc=current;return 1;int Creat
5、eDG(ALGraphint i;for( i=0; iv1v2info;ArcNode* current=G.verticesv1-1.firstarc;ArcNode* p=G.verticesv1-1.firstarc;InDegreev2-1+;if(current=NULL)G.verticesv1-1.firstarc=new ArcNode;G.verticesv1-1.firstarc-adjvex=v2-1;G.verticesv1-1.firstarc-nextarc=NULL;G.verticesv1-1.firstarc-info=info;elsewhile(curr
6、ent!=NULL)p=current;current=current-nextarc;current=new ArcNode;current-adjvex=v2-1;current-nextarc=NULL;current-info=info;p-nextarc=current;return 1;int CreateDG1(ALGraphint i;for( i=0; iv1v2;ArcNode* current=G.verticesv2-1.firstarc;ArcNode* p=G.verticesv2-1.firstarc;if(current=NULL)G.verticesv2-1.
7、firstarc=new ArcNode;G.verticesv2-1.firstarc-adjvex=v1-1;G.verticesv2-1.firstarc-nextarc=NULL;elsewhile(current!=NULL)p=current;current=current-nextarc;current=new ArcNode;current-adjvex=v1-1;current-nextarc=NULL;p-nextarc=current;return 1;int CreateGraph(ALGraphswitch(G.kind)case 0:return CreateUG(
8、G); /构造无向图case 1:return CreateDG(G); /构造有向图case 2:return CreateDG1(G); /构造逆邻接图default:return 0;int veMAX_VERTEX_NUM=0;bool TopologicalSort(ALGraph int i;for( i=0; inextarc)int w=p-adjvex;InDegreew-;if(InDegreew=0)S.push(w);if(vei+p-info vew)vew=vei+p-info;if(count T;if(!TopologicalSort(G,T)return ;int v1MAX_VERTEX_NUM;for( k=0; knextarc)k=p-adjvex;dut=p-info;if(v1k-dutnextarc)k=q-adjvex; dut=q-info;if(vej=v1k-dut)coutadjvexnextarc;coutnextarc;delete current1;current1=p;return 1;