天76 2022-06-01 23:33 采纳率: 100%
浏览 22
已结题

大一提问 我代码在topologicalsort的这个自定义函数内运行卡住了,大伙们帮我看看有什么问题 加v13617479134

#include<stdio.h>#includeusing namespace std;#define MVNum 100#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;typedef int VerTexType; //顶点的数据类型为整型typedef struct ArcNode //边结点{ int adjvex; //该边所指向的顶点的位置 struct ArcNode *nextarc; //指向下一条边的指针 int info; //和边相关的信息}ArcNode;typedef struct VNode //顶点信息{ VerTexType data; ArcNode *firstarc; //指向第一条依附该顶点的边的指针}VNode,AdjList[MVNum]; //AdjList表示邻接表类型typedef struct //邻接表{ AdjList vertices; int vexnum,arcnum; //图的当前顶点数和边数}ALGraph;Status LocateVex(ALGraph G,int u){ int i; for(i=0;i<G.vexnum;++i) { if(u==G.vertices[i].data) return i; } return ERROR;}Status CreateUDG(ALGraph &G){ int i,k,j,w;int v1,v2;ArcNode *p1; printf("请依次输入总顶点数和总边数\n"); scanf("%d%d",&G.vexnum,&G.arcnum); printf("请输入点的data值,如v0只取其下标的数字\n"); getchar(); for(i=0;i<G.vexnum;++i) {printf("请输入第%d个点的名称:",i+1); scanf("%d",&G.vertices[i].data); G.vertices[i].firstarc=NULL;} for(k=0;k<G.arcnum;++k) { printf("请输入第%d条边依附的顶点及其权值,格式如0 1 6, 01为顶点0指向1,6为权值:",k+1); scanf("%d%d%d",&v1,&v2,&w); i=LocateVex(G,v1);j=LocateVex(G,v2); p1=new ArcNode; p1->adjvex=j; p1->info=w; p1->nextarc=G.vertices[i].firstarc; G.vertices[i].firstarc=p1; } return OK;}typedef struct StackNode{ int data; struct StackNode *next;}StackNode,*LinkStack;Status InitStack(LinkStack &S){ S=NULL; return OK;}bool StackEmpty(LinkStack S) { if (!S) return true; return false;}Status Push(LinkStack &S,int e){ StackNode p; p=new StackNode; p->data=e; p->next=S; S=p; return OK;}Status Pop(LinkStack &S,int &e){ StackNodep; if(S==NULL) return ERROR; e=S->data; p=S; S=S->next; delete p; return OK;}void FindInDegree(ALGraph G,int indegree[]){ ArcNode *p; for(int i=0;i<G.vexnum;i++) indegree[i]=0; for(int i=0;i<G.vexnum;i++) { p=G.vertices[i].firstarc; while(p) { indegree[p->adjvex]++; p=p->nextarc; } }}Status TopologicalSort(ALGraph G,int topo[]){//有向图G采用邻接表存储结构 //若G无回路,则G生成G的一个拓扑排序topo[]并返回OK,否则ERROR int i,k,m; int indegree[100]; LinkStack S;ArcNode *p; FindInDegree(G,indegree); InitStack(S); for(i=0;i<G.vexnum;++i) if(!indegree[i]) Push(S,i); m=0; while(!StackEmpty(S)) { printf("20"); Pop(S,i);printf("10"); printf("60");topo[m]=i;printf("40"); ++m;printf("30"); p=G.vertices[i].firstarc; while(p!=NULL) { k=p->adjvex; --indegree[k]; if(indegree[k]==0) Push(S,k); p=p->nextarc; } } if(m<G.vexnum) return ERROR; else return OK;}int *topo;int *ve;int *vl;int e,l;Status CriticalPath(ALGraph G){//G为邻接表储存的有向表,输出G的各项关键活动 int i,j,k,n; ArcNode *p; if(!TopologicalSort(G,topo)) {printf("30"); return ERROR;} //调用拓扑排序算法,使拓扑排序保存在topo中,若调用失败,则存在有向环,返回ERROR n=G.vexnum; printf("70"); //n为顶点个数 for(i=0;i<n;i++) //给每个事件的最早发生时间置初值0 ve[i]=0; /*按拓扑次序求每个事件的最早发生时间*/ for(i=0;i<n;i++) { k=topo[i]; //取得拓扑序列中的顶点序号k while(p!=NULL) { j=p->adjvex; if(ve[j]<ve[k]+p->info) ve[j]=ve[k]+p->info; p=p->nextarc; } } for(i=0;i<n;i++) vl[i]=ve[n-1]; /*按拓扑次序求每个事件的最迟发生时间*/ for(i=n-1;i>=0;i--) { k=topo[i]; p=G.vertices[k].firstarc; while(p!=NULL) { j=p->adjvex; if(vl[k]>vl[j]-p->info) vl[k]=vl[j]-p->info; p=p->nextarc; } } /判断每一活动是否为关键活动/ for(i=0;i<n;i++) { p=G.vertices[i].firstarc; while(p!=NULL) { j=p->adjvex; e=ve[i]; l=vl[j]-p->info; if(e==1) cout<<G.vertices[i].data<<G.vertices[j].data; p=p->nextarc; } } printf("90");}bool visited[MVNum];void DFS_AL(ALGraph G,int v){ ArcNode *P; int w; printf("%d",G.vertices[v].data); visited[v]=true; P=G.vertices[v].firstarc; while(P!=NULL) { w=P->adjvex; if(!visited[w]) DFS_AL(G,w); P=P->nextarc; }}int main(){ ALGraph G; CreateUDG(G); CriticalPath(G);printf("%d",ve[0]);} 太乱了,加我微信13617479134

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 6月2日
    • 修改了问题 6月1日
    • 修改了问题 6月1日
    • 修改了问题 6月1日
    • 展开全部

    悬赏问题

    • ¥15 QWebEngineView
    • ¥15 .net core 同时编辑怎么防止数据串了
    • ¥20 微信小程序播放直播流
    • ¥15 关于迷宫自走单片机循迹小车的知识
    • ¥15 python使用selenium工具爬取网站的问题
    • ¥15 visual studio中c语言用ODBC链接SQL SERVER
    • ¥15 关于#python#的问题:如何通过pywinauto获取到图中“窗格”内部的内容
    • ¥15 visionMaster4.3.0 与QT 的二次开发异常
    • ¥50 关于#pcb工艺#的问题:这个设计电路中,最终组合起来起到了什么作用
    • ¥15 鼎捷t100或鼎捷E10生产模块与odoo17详细区别和鼎捷t100或鼎捷E10物料清单(BOM)可以在子级的子级在同一界面操作吗