资源描述:
《关键 路径代码算法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#definestack_max_size100#definestack_extra10typedefstruct{int*top;int*base;intstacksize;}vexstack,*pvexstack;intinitstack(vexstack&st){st.base=(int*)malloc(stack_max_size*sizeof(int));if(!st.base)exit(0);st.top=st.base;st.
2、stacksize=stack_max_size;return1;}intpop(vexstack&st,int&a){if(st.top==st.base)return0;a=*(--st.top);return1;}intpush(vexstack&st,inta){if(st.top-st.base>=st.stacksize){st.base=(int*)realloc(st.base,(stack_max_size+stack_extra)*sizeof(int));if(!st.base)exit
3、(0);st.top=st.base+st.stacksize;st.stacksize+=stack_extra;}*st.top++=a;return1;}intdestroystack(vexstack&st){free(st.base);return1;}intemptystack(vexstackst){if(st.top==st.base)return1;elsereturn0;}typedefstructnarc{intadjvex;intinfo;}narc,*pnarc;typedefstr
4、uctheadvexter{intindegree;intkaiguan;intoutdegree;chardata;pnarcoutarc;}headvexter,*pheadvexter;typedefstruct{intvexnum;pheadvextervexs;}graph,*pgraph;voidinitgraph(graph&G){inti=0,j,k=0,n,l=0;charc[100];printf("请输入有向图的节点:");gets(c);while(c[i]!=' '){i++;
5、}G.vexnum=i;G.vexs=(pheadvexter)malloc((i+1)*sizeof(headvexter));i=0;while(c[i]!=' '){G.vexs[i+1].data=c[i];G.vexs[i+1].kaiguan=0;i++;}n=i;for(j=1;j<=n;j++){printf("请输入节点%c的入度:",G.vexs[j].data);scanf("%d",&G.vexs[j].indegree);printf("请输入节点%c的出度:",G.vexs[j]
6、.data);scanf("%d",&G.vexs[j].outdegree);G.vexs[j].outarc=(pnarc)malloc((G.vexs[j].outdegree+1)*sizeof(narc));for(i=1;i<=G.vexs[j].outdegree;i++){printf("%c节点的第%d条弧对应的邻接点所对应的序号为:",G.vexs[j].data,i);scanf("%d",&G.vexs[j].outarc[i].adjvex);printf("%c节点的第%d条弧所带
7、的信息为:",G.vexs[j].data,i);scanf("%d",&G.vexs[j].outarc[i].info);}}}inttopologicalorder(graphG,vexstack&s,int*&ve,int&max){inti=1,j,k,count=0;ve=(int*)malloc((G.vexnum+1)*sizeof(int));while(i<=(G.vexnum+1)){ve[i++]=0;}for(i=1;i<=G.vexnum;i++){if((G.vexs[i].in
8、degree==0)&&G.vexs[i].kaiguan==0){push(s,i);G.vexs[i].kaiguan=1;count++;}for(j=1;j<=G.vexs[i].outdegree;j++){k=G.vexs[i].outarc[j].adjvex;if(ve[i]+G.vexs[i].outarc[j].info>ve[k]){ve[k]=ve[i]+G.vexs[i].