关键路径_代码

关键路径_代码_哔哩哔哩_bilibili

 

//关键路径 O(V+E) C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define MAXV 100  //最大顶点数
#define MAXA 1000  //最大弧数

//------------------------链栈
typedef struct LiNode{
    int data;
    struct LiNode *next;
}LiNode,*LiStack;

//初始化栈
void InitStack(LiStack &L){
    L=(LiNode*)malloc(sizeof(LiNode));
    L->next=NULL;
}

//判断栈是否为空
bool StackEmpty(LiStack L){
    return L->next==NULL;
}

//入栈
void Push(LiStack &L,int e){
    LiNode *s=(LiNode*)malloc(sizeof(LiNode));
    s->data=e;
    s->next=L->next;
    L->next=s;
}

//出栈
void Pop(LiStack &L){
    if(StackEmpty(L)) return;
    LiNode *s=L->next;
    L->next=s->next;
    free(s);
}

//读栈顶
int Top(LiStack L){
    if(!StackEmpty(L))
        return L->next->data;
}

//---------------------------------邻接表
typedef struct ArcNode{ //边结点
    int adjvex; //该弧指向的顶点下标
    int weight; //边权
    int flag;   //关键活动标志
    struct ArcNode *nextarc; //指向下一条弧
}ArcNode;

typedef struct VexNode{ //顶点结点
    char data; //顶点信息
    ArcNode *firstarc; //指向该顶点的第一条弧
}VexNode;

typedef struct{
    VexNode AdjList[MAXV]; //邻接表
    int n,m; //顶点数,弧数
    ArcNode *curs[MAXA]; //存储关键活动的指针
    int count; //关键活动计数器
}ALGraph;

//用栈返回拓扑序列,参数ve数组返回各顶点的最早发生时间
LiStack TopoSort(ALGraph G, int *ve){
    ArcNode *p; int i,j,*indegree,count=0;
    indegree=(int*)calloc(G.n,sizeof(int)); //初始化为0
    for(i=0; i<G.n; i++) //计算每个顶点的入度              
        for(p=G.AdjList[i].firstarc; p; p=p->nextarc)
            indegree[p->adjvex]++;
            
    LiStack S,T;
    InitStack(S); InitStack(T);          
    for(i=0; i<G.n; i++) //入度为0的顶点入栈
        if(indegree[i]==0) Push(S,i);
    while(!StackEmpty(S)){
        i=Top(S); Pop(S); //取出栈顶元素
        Push(T,i); //用栈T记录拓扑序
        count++;   //拓扑点计数
        for(p=G.AdjList[i].firstarc; p; p=p->nextarc){
            j=p->adjvex; //取i的邻接顶点
            if(--indegree[j]==0) Push(S,j);
            if(ve[j]<ve[i]+p->weight) //更新最早发生时间ve[j]
                ve[j]=ve[i]+p->weight;
        }
    }
    free(indegree); //回收辅助数组
    if(count<G.n) return NULL; //存在回路,返回NULL报错
    else return T;
}

//求有向网G的关键活动,并在其弧结点打标志
void CriticalPath(ALGraph G){
    LiStack S; ArcNode *p; int i,j,*ve,*vl;
    ve=(int *)calloc(G.n,sizeof(int)); //最早发生时间数组
    vl=(int *)malloc(G.n*sizeof(int)); //最迟发生时间数组
    S=TopoSort(G,ve); //求得拓扑序列栈和ve值
    for(i=0; i<G.n; i++) vl[i]=ve[G.n-1]; //对vl初始化
    while(!StackEmpty(S)){
        i=Top(S); Pop(S); //逆拓扑序出栈
        for(p=G.AdjList[i].firstarc; p; p=p->nextarc){
            j=p->adjvex; //取i的邻接顶点
            if(vl[i]>vl[j]-p->weight) //更新最迟发生时间vl[i]
                vl[i]=vl[j]-p->weight;
        }
    }
    for(i=0; i<G.n; i++){
        for(p=G.AdjList[i].firstarc; p; p=p->nextarc){
            if(ve[i]==vl[p->adjvex]-p->weight) //若最早和最晚开始时间相等
                p->flag=1; //则在弧结点打关键活动标志
        }
    }
    free(ve); free(vl); //回收辅助数组
}

//从p弧起递归深搜AOE网G,输出关键路径
void DFS(ALGraph G, ArcNode *p){
    while(p){
        if(p->flag!=1) p=p->nextarc; //跳过非关键活动(弧)
        else{                        //对关键活动做以下处理
            G.curs[G.count++]=p;     //把关键活动的指针存入数组
            if(!G.AdjList[p->adjvex].firstarc){ //若是汇点
                printf("%c",G.AdjList[0].data); //输出源点
                for(int i=0; i<G.count; i++)    //依次输出关键活动,直到汇点
                    printf(" -%d->%c",G.curs[i]->weight,G.AdjList[G.curs[i]->adjvex].data);
                printf("\n");
            }
            else DFS(G,G.AdjList[p->adjvex].firstarc); //非汇点则递归
            G.count--; p=p->nextarc; //回退一个关键活动,p指向下一出弧
        }
    }
}

//对已求得关键路径的AOE网G深搜,显示关键路径
void PrintCritPath(ALGraph G){   
    G.count=0; //清0,用作关键活动计数器
    DFS(G,G.AdjList[0].firstarc); //从源点第一条出弧开始深搜
}

int main(){
    ALGraph G; ArcNode *p;
    G.n=4;
    G.m=5;
    char vex[]={'A','B','C','D'};
    for(int i=0;i<G.n;i++)
        G.AdjList[i].data=vex[i]; //顶点data
        
    int e[5][3]={ //源点编号0,汇点编号n-1
        0,2,5, //AC    
        0,1,2, //AB
        2,3,7, //CD        
        2,1,1, //CB
        1,3,6, //BD
    };
    for(int i=0;i<G.m;i++){
        p=(ArcNode*)malloc(sizeof(ArcNode));
        p->nextarc=G.AdjList[e[i][0]].firstarc;
        G.AdjList[e[i][0]].firstarc=p; //头插法
        p->adjvex=e[i][1];
        p->weight=e[i][2];
    }
    CriticalPath(G);  //处理关键路径
    PrintCritPath(G); //输出关键路径
    return 0;
}

 

.

posted @ 2026-10-27 08:36  董晓  阅读(14)  评论(0)    收藏  举报