| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 363 人关注过本帖
标题:关于图的关键路径问题
只看楼主 加入收藏
apigboy
Rank: 1
等 级:新手上路
帖 子:35
专家分:4
注 册:2011-10-3
结帖率:85.71%
收藏
 问题点数:0 回复次数:0 
关于图的关键路径问题
我写了一个计算图的关键路径的程序,可是出来的最早和最晚老是一样的,关键活动也不对,高手帮忙看看那里错了
程序代码:
#include "iostream.h"
#include "stdlib.h"
#include "malloc.h"
#define MAX 30

typedef struct node
{
    int vex;
    struct node *next;
}edgenode;

typedef struct vnode
{
    int id;
    struct node *link;
}vexnode;

typedef struct nodel
{
    int adgvex;
    int dut;
    struct nodel *next;
}edgenodel;

typedef struct vnodel
{
    int vertex;
    int id;
    struct nodel *link;
}vexnodel;

int na;
int creatAOE(vexnodel dig[]);
int creatAOV(vexnode dig[]);
void toposort(vexnode dig[],int n);
void criticalpath(vexnodel dig[],int n);

void main()
{
    vexnodel dig[MAX];
    vexnode dig2[MAX];
    int n=0;
    int cord=1;
    while (1)
    {
        cout<<"     主菜单     "<<endl;
        cout<<"1.建立AOV邻接链表"<<endl;
        cout<<"2.建立AOE邻接链表"<<endl;
        cout<<"3.拓扑排序"<<endl;
        cout<<"4.求关键路径"<<endl;
        cout<<"输入代码选择:(1-4),0表示结束"<<endl;
        cin>>cord;
        switch(cord)
        {
        case 1: 
            n=creatAOV(dig2);
            break;
        case 2:    
            cout<<"input Arcs:";
            cin>>na;
            n=creatAOE(dig);
            break;
        case 3:
            toposort(dig2,n);
            break;
        case 4:
            criticalpath(dig,n);
            break;
        case 0:
            exit(9);
        }
    }
}

int creatAOE(vexnodel dig[])
{
    int vextotal,j,a,b,c;
    edgenodel *p;
    cout<<"input vex number:";
    cin>>vextotal;
    for(j=0;j<MAX;j++)
    {
        dig[j].vertex=j;
        dig[j].id=0;
        dig[j].link=NULL;
    }
    for(j=1;j<=na;j++)
    {
        cout<<"input edge<i,j>weight:";
        cin>>a>>b>>c;
        dig[b].id++;
        p=(edgenodel*)malloc(sizeof(edgenodel));
        p->adgvex=b;
        p->dut=c;
        p->next=dig[a].link;
        dig[a].link=p;
    }
    cout<<"output link table"<<endl;
    for(j=1;j<=vextotal;j++)
    {
        cout<<j<<"   "<<dig[j].id<<"   ";
        p=dig[j].link;
        while (p!=0)
        {
            cout<<p->adgvex<<"   "<<p->dut<<"   ";
            p=p->next;
        }
        cout<<endl;
    }
    return(vextotal);
}

int creatAOV(vexnode dig[])
{
    int vextotal,arcnumber,j,a,b;
    edgenode *p;
    cout<<" input vex number:";
    cin>>vextotal;
    cout<<" input Arc number:";
    cin>>arcnumber;
    for(j=0;j<MAX;j++)
    {
        dig[j].id=0;
        dig[j].link=NULL;
    }
    for(j=1;j<=arcnumber;j++)
    {
        cout<<" input vex number<i,j>:"<<endl;
        cin>>a>>b;
        dig[b].id++;
        p=(edgenode*)malloc(sizeof(edgenode));
        p->vex=b;
        p->next=dig[a].link;
        dig[a].link=p;
    }
    cout<<" output link table"<<endl;
    for(j=1;j<=vextotal;j++)
    {
        cout<<j<<"   "<<dig[j].id<<"   ";
        p=dig[j].link;
        while (p!=NULL)
        {
            cout<<p->vex<<"   ";
            p=p->next;
        }
        cout<<endl;
    }
    return(vextotal);
}

void toposort(vexnode dig[],int n)
{
    edgenode *p;
    int i,j,k,top,m;
    top=0;
    m=0;
    for(i=0;i<n;i++)
    {
        if (dig[i].id==0)
        {
            dig[i].id=top;
            top=i;
        }
    }
    while (top>0)
    {
        j=top;
        top=dig[top].id;
        cout<<j;
        m++;
        p=dig[j].link;
        while (p!=NULL)
        {
            k=p->vex;
            dig[k].id--;
            if (dig[k].id==0)
            {
                dig[k].id=top;
                top=k;
            }
            p=p->next;
            }
        cout<<"  ";
    }
    if (m<n)
        cout<<" The network has acycle."<<endl;
}

void criticalpath(vexnodel dig[],int n)
{
    int front=0,rear=0;
    int tpord[20],vl[20],ve[20];
    int l[20],e[20],i,j,k,m;
    edgenodel *p;
    for(i=1;i<=n;i++) ve[i]=0;
    for(i=1;i<=n;i++)
        if(dig[i].id==0) tpord[++rear]=i;
    m=0;
    while (front!=rear)
    {
        front++;
        j=tpord[front];
        m++;
        p=dig[j].link;
        while (p)
        {
            k=p->adgvex;
            dig[k].id--;
            if(dig[k].id==0) tpord[++rear]=k;
            if(ve[j]+p->dut>ve[k]) ve[k]=ve[j]+p->dut;
            p=p->next;
        }
    }
    if (m<n) cout<<" The AOE network has acycle"<<endl;
    for(i=1;i<=n;i++) vl[i]=ve[i];
    for(i=n;i>0;i--)
    {
        j=tpord[i];
        p=dig[j].link;
        while (p)
        {
            k=p->adgvex;
            if ((vl[k]-p->dut)<vl[j]) vl[j]=vl[k]-p->dut; 
            p=p->next;
        }
    }
    cout<<" output VE VL"<<endl;
    for(i=1;i<=n;i++)
        cout<<ve[i]<<"   "<<vl[i]<<endl;
    for(j=1,i=1;j<=n;j++,i++)
    {
        p=dig[j].link;
        while (p)
        {
            k=p->adgvex;
            e[i]=ve[j];
            l[i]=vl[k]-p->dut;
            cout<<dig[j].vertex<<"   "<<dig[k].vertex<<"   "<<e[i]<<"   "<<l[i]<<"   "<<l[i]-e[i];
            if (l[i]==e[i]) 
                cout<<"    CRITICAL ACTIVITY"<<endl;
            else
                cout<<endl;
            p=p->next;
        }
    }
}
搜索更多相关主题的帖子: color 
2011-11-22 20:35
快速回复:关于图的关键路径问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027525 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved