#include<iostream> #include<string> #include<stack> #include<assert.h> using namespace std; #define Max 100 int ve[Max]; int vl[Max]; int ee,el; int eetemp[Max*Max],eltemp[Max*Max]; int indegree[Max];//入度 int flag = 0; int a; stack<int>stk; stack<int>restk; typedef struct Gnode { int adjvex;//邻接点域 int weight;//权重 struct Gnode * nextarc;//链域 }Gnode; typedef struct Adjlist { string data; struct Gnode * firstarc;//头结点的链域 }Adjlist; typedef struct Graph { int vn,en;//顶点和边的数目 struct Adjlist head[Max];//头结点数组 }Graph; int locate_vex(Graph & graph , string v) { for(int i = 0; i < graph.vn; i ++) if(graph.head[i].data == v) return i; return -1; } void build_graph(Graph & graph) { cout<<"请输入图的顶点数"<<endl; cin>>graph.vn; cout<<"请输入图的边数"<<endl; cin>>graph.en; cout<<"输入各顶点的信息"<<endl; for(int k = 0; k < graph.vn; k++) { graph.head[k].data = " "; graph.head[k].firstarc = NULL; } for( int i = 0; i < graph.vn; i++ ) { cout<<"顶点下标为"<<i<<"的信息"<<endl; cin>>graph.head[i].data; } cout<<"输入活动所依附的事件信息(如下标为0的顶点信息为v0)"<<endl; for( int l = 0; l < graph.en; l++ ) { string str1,str2; cout<<"弧尾事件:"; cin>>str1; cout<<"弧头事件:"; cin>>str2; cout<<"活动所需时间:"; int w; cin>>w; int i = locate_vex(graph,str1); int j = locate_vex(graph,str2); Gnode * p = (Gnode *)malloc(sizeof(Gnode)); Gnode * q = (Gnode *)malloc(sizeof(Gnode)); q->nextarc = NULL; assert(p != NULL); if(graph.head[i].firstarc == NULL) { graph.head[i].firstarc = p; } else q = graph.head[i].firstarc; while(q->nextarc != NULL ) { q = q->nextarc; } q->nextarc = p; p->adjvex = j; p->weight = w; p->nextarc = NULL; }//for }//build_graph void Display(Graph G) { int i; Gnode *p; cout<<"该图为:"<<endl; for(i=0;i<G.vn;i++) { p=G.head[i].firstarc; while(p!=NULL) { cout<<"边("<<i<<","<<p->adjvex<<")"<<"活动时间"<<p->weight<<endl; p=p->nextarc; } } cout<<endl; } int top(Graph g) { Gnode * p = (Gnode *)malloc(sizeof(Gnode)); for(int i=0; i<g.vn; i++) { ve[i] = 0; p = g.head[i].firstarc; for(p; p!=NULL; p=p->nextarc) { indegree[p->adjvex]++; } } for(int k=0; k<g.vn; k++) { cout<<"事件"<<g.head[k].data<<" "<<"入度为"<<indegree[k]<<endl; } cout<<endl; int nCount=0; for(int j=0; j<g.vn; j++) { if(indegree[j] == 0) stk.push(j); } cout<<"拓扑顺序:"; while(!stk.empty ()) { a = stk.top(); cout<<g.head[a].data<<" "; stk.pop(); nCount ++; restk.push(a); for(p=g.head[a].firstarc; p!=NULL; p=p->nextarc) { indegree[p->adjvex]--; if(indegree[p->adjvex] == 0) stk.push(p->adjvex); if(ve[a]+p->weight > ve[p->adjvex ]) ve[p->adjvex] = ve[a] + p->weight; } } cout<<endl;cout<<endl; if(nCount < g.vn) { flag = 1; return 0; } else return 1; } void AOE(Graph & g) { if(flag == 1) { cout<<"图中有环!"<<endl; return; } for(int i=0; i<g.vn; i++) { vl[i] = ve[g.vn-1]; } Gnode * p = (Gnode *)malloc(sizeof(Gnode)); cout<<"工程能完成的最早时间为"<<ve[g.vn-1]<<endl; cout<<endl; while(!restk.empty()) { int t = restk.top(); restk.pop(); for(p=g.head[t].firstarc; p!=NULL; p=p->nextarc) { if(vl[p->adjvex]-p->weight < vl[t]) vl[t] = vl[p->adjvex] - p->weight; } } for(int j=0; j<g.vn; j++) for(p=g.head[j].firstarc; p!=NULL; p=p->nextarc) { ee = ve[j]; el = vl[p->adjvex] - p->weight; cout<<"弧尾依附事件"<<g.head[j].data<<'/t'<<"弧头依附事件"<<g.head[p->adjvex].data <<"的活动:"<<endl; cout<<"----------------------------------------------------------------------"<<endl; cout<<"最早开始时间是"<<ee<<'/t'<<"最迟开始时间是"<<el; if(ee == el) { cout<<'/t'<<"该活动是关键活动"; } cout<<endl; cout<<"**********************************************************************"<<endl; } } int main() { Graph G; build_graph(G); Display(G); top(G); AOE(G); return 0; }