关键路径 C实现

#include "stdafx.h"
#include 
#include 

#define MAX_VERTEX_NUM 20

typedef struct ArcNode
{
    int data;
    int weight;
    struct ArcNode *next;
}ArcNode, *PArcNode;

typedef struct VNode
{
    int indegree;
    char vertex;
    PArcNode first;
}VNode, AdjList[MAX_VERTEX_NUM];

typedef struct ALGraph
{
    int arcnum, vexnum;
    AdjList adjlist;
}ALGraph, *PALGraph;


int GetLocation(PALGraph palgraph, char v)
{
    for (int i = 0; i < palgraph->vexnum; i++)
    {
        if (v == palgraph->adjlist[i].vertex)
            return i;
    }
    return -1;
}


PALGraph CreateGraph()
{
    PALGraph palgraph = (PALGraph)malloc(sizeof(ALGraph));
    printf("输入定点数和边数:");
    scanf("%d %d", &palgraph->vexnum, &palgraph->arcnum);
    getchar();
    printf("输入每个顶点名称\n");
    for (int i = 0; i < palgraph->vexnum; i++)
    {
        printf("输入第%d个顶点名称:",i);
        scanf("%c", &palgraph->adjlist[i].vertex);
        getchar();
        palgraph->adjlist[i].first = NULL;
        palgraph->adjlist[i].indegree = 0;
    }

    printf("输入边的起始点及边的权值:\n");
    char u, v;
    int p, q, w;
    for (int i = 0; i < palgraph->arcnum; i++)
    {
        printf("输入第%d条边起始点:",i);
        scanf("%c %c %d", &u, &v,&w);
        getchar();
        p = GetLocation(palgraph, u);
        q = GetLocation(palgraph, v);
        if (p != -1 && q != -1)
        {
            PArcNode parcnode = (PArcNode)malloc(sizeof(ArcNode));
            parcnode->data = q;
            parcnode->next = NULL;
            parcnode->weight = w;

            if (palgraph->adjlist[p].first == NULL)
            {
                palgraph->adjlist[p].first = parcnode;
                palgraph->adjlist[q].indegree += 1;

            }
            else
            {
                PArcNode parc = palgraph->adjlist[p].first;
                while (parc->next != NULL)
                {
                    parc = parc->next;
                }
                parc->next = parcnode;
                palgraph->adjlist[q].indegree += 1;
            }
        }   
    }
    return palgraph;
}


typedef struct SNode
{
    int sdata;
    struct SNode *next;
}SNode, *PSNode;

typedef struct Stack
{
    PSNode top;
    PSNode base;
}Stack,*PStack;

PStack InitStack()
{
    PStack pstack = (PStack)malloc(sizeof(Stack));
    PSNode psnode = (PSNode)malloc(sizeof(SNode));
    psnode->next = NULL;
    pstack->base = psnode;
    pstack->top = psnode;
    return pstack;
}


bool StackEmpty(PStack pstack)
{
    if (pstack->base == pstack->top)
        return true;
    return false;
}


void Push(PStack pstack, int index)
{
    PSNode psnode = (PSNode)malloc(sizeof(SNode));
    psnode->sdata = index;
    psnode->next = pstack->top;
    pstack->top = psnode;
}


int Pop(PStack pstack)
{
    if (!StackEmpty(pstack))
    {
        PSNode psnode = pstack->top;
        pstack->top = psnode->next;
        int data = psnode->sdata;
        free(psnode);
        return data;
    }
    else{
        printf("Stack is Empty!\n");
        return -1;
    }
}


int ve[MAX_VERTEX_NUM];     //事件的最早开始时间
int vl[MAX_VERTEX_NUM];     //事件的最迟开始时间
PStack P;     //求解最早开始时间使用
PStack T;     //求解最迟开始时间使用

bool TopologocalOrder(PALGraph palgraph)
{
    int count = 0,index, k;
    P = InitStack();
    T = InitStack();
    for (int i = 0; i < palgraph->vexnum; i++)
    {
        if (palgraph->adjlist[i].indegree == 0)
        {
            Push(P, i);
        }
        ve[i] = 0;
    }

    while (!StackEmpty(P))
    {
        index = Pop(P);
        Push(T, index);
        count++;

        PArcNode p;
        for (p = palgraph->adjlist[index].first; p != NULL; p = p->next)
        {
            k = p->data;
            if ((--palgraph->adjlist[k].indegree) == 0)
                Push(P, k);

            if (ve[k] < ve[index] + p->weight)
                ve[k] = ve[index] + p->weight;
        }
    }

    if (count < palgraph->vexnum)
        return false;

    return true;
}


bool CriticalPath(PALGraph palgraph)
{
    if (!TopologocalOrder(palgraph))
        return false;

    for (int i = 0; i < palgraph->vexnum; i++)
    {
        vl[i] = ve[palgraph->vexnum - 1];
    }

    while (!StackEmpty(T))
    {
        int j = Pop(T);
        for (PArcNode p = palgraph->adjlist[j].first; p != NULL; p = p->next)
        {
            if (vl[p->data] - p->weight < vl[j])
                vl[j] = vl[p->data] - p->weight;
        }
    }

    int ee, el,j,k;
    PArcNode p;
    for (int j = 0; j < palgraph->vexnum; j++)
    {
        for (p = palgraph->adjlist[j].first; p != NULL; p = p->next)
        {
            k = p->data;
            ee = ve[j];
            el = vl[k] - p->weight;
            if (ee == el)
                printf("%d - %d  %c--%c %d\n", j, k, palgraph->adjlist[j].vertex, palgraph->adjlist[k].vertex, p->weight);
        }
    }
    return true;
}


int main()
{
    PALGraph palgraph = CreateGraph();
    if (!CriticalPath(palgraph))
        printf("error\n");
}

你可能感兴趣的:(数据结构)