// exam1.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std; #define MAXVEX 20 typedef struct ArcNode
{
int adjvex;
int weight;
struct ArcNode* nextarc;
}ArcNode; typedef struct HeadNode
{
int data;
ArcNode* firstarc;
}HeadNode; typedef struct ALGraph
{
int arcnum;
int vexnum;
int* ve;
int* vl;
HeadNode vex[MAXVEX];
}ALGraph; void InitALGraph(ALGraph& G)
{
cout<<"please enter the number of the vertex:"<<endl;
cin>>G.vexnum; G.arcnum=0;
for(int i=1;i<G.vexnum+1;i++)
{
G.vex[i].data=i;
G.vex[i].firstarc=NULL;
} cout<<"please enter the Arc..."<<endl;
cout<<"head tail weight"<<endl;
int adj1,adj2,w;
while(cin>>adj1>>adj2>>w)
{
G.arcnum++;
ArcNode* arc;
arc=G.vex[adj1].firstarc;
if(arc==NULL)
{
G.vex[adj1].firstarc=(ArcNode*)malloc(sizeof(ArcNode));
arc=G.vex[adj1].firstarc;
arc->adjvex=adj2;
arc->weight=w;
arc->nextarc=NULL;
}
else
{
while(arc->nextarc)
{
arc=arc->nextarc;
}
arc->nextarc=(ArcNode*)malloc(sizeof(ArcNode));
arc->nextarc->adjvex=adj2;
arc->nextarc->weight=w;
arc->nextarc->nextarc=NULL;
}
} for(int i=1;i<G.vexnum+1;i++)
{
cout<<"vextex"<<i<<"'s adjacent vertices are:";
ArcNode* arc;
arc=G.vex[i].firstarc;
while(arc!=NULL)
{
cout<<arc->adjvex<<"-"<<arc->weight<<" ";
arc=arc->nextarc;
}
cout<<endl;
}
} void CalIndegree(ALGraph G,int* indegree)
{
for(int i=1;i<G.vexnum+1;i++)
{
ArcNode* arc;
arc=G.vex[i].firstarc;
while(arc!=NULL)
{
indegree[arc->adjvex]++;
arc=arc->nextarc;
}
}
} int TopologicalOrder(ALGraph &G,stack<int> &TNode)
{
int *indegree=(int*)malloc(sizeof(int)*(G.vexnum+1));
memset(indegree,0,sizeof(int)*(G.vexnum+1));
G.ve=(int*)malloc(sizeof(int)*(G.vexnum+1));
memset(G.ve,0,sizeof(int)*(G.vexnum+1)); CalIndegree(G,indegree);
stack<int> s; for(int i=1;i<G.vexnum+1;i++)
{
if(indegree[i]==0)
{
s.push(i);
}
} cout<<"The topological sort of the graph is"<<endl;
int count=0;
while(!s.empty())
{
int i=s.top();
TNode.push(i);
s.pop();
cout<<i<<" ";
count++; ArcNode* arc;
arc=G.vex[i].firstarc;
while(arc!=NULL)
{
if(G.ve[i]+arc->weight>G.ve[arc->adjvex])
{
G.ve[arc->adjvex]=G.ve[i]+arc->weight;
} if(--indegree[arc->adjvex]==0)
{
s.push(arc->adjvex);
}
arc=arc->nextarc;
}
} cout<<endl;
if(count==G.vexnum)
{
cout<<"No loop in the graph..."<<endl;
}
else
{
cout<<"There are a loop in the graph..."<<endl;
return 0;
} cout<<"The early staring time is:"<<endl;
for(int i=1;i<G.vexnum+1;i++)
{
cout<<G.ve[i]<<" ";
}
cout<<endl; return 1;
} void CriticalPath(ALGraph G)
{
G.vl=(int*)malloc(sizeof(int)*(G.vexnum+1));
memset(G.vl,0,sizeof(int)*(G.vexnum+1));
stack<int> s; if(TopologicalOrder(G,s))
{
for(int i=1;i<G.vexnum+1;i++)
{
G.vl[i]=G.ve[G.vexnum];
}
while(!s.empty())
{
int i=s.top();
s.pop(); ArcNode* arc;
arc=G.vex[i].firstarc; while(arc!=NULL)
{
int k=arc->adjvex;
if(G.vl[k]-arc->weight<G.vl[i])
{
G.vl[i]=G.vl[k]-arc->weight;
}
arc=arc->nextarc;
}
}
}
else
{
return;
} cout<<"The laster starting time is:"<<endl;
for(int i=1;i<G.vexnum+1;i++)
{
cout<<G.vl[i]<<" ";
}
cout<<endl; cout<<"The critical path of the graph is:"<<endl;
for(int i=1;i<G.vexnum+1;i++)
{
ArcNode* arc;
arc=G.vex[i].firstarc;
while(arc!=NULL)
{
int k=arc->adjvex;
if(G.ve[i]==G.vl[k]-arc->weight)
{
cout<<G.vex[i].data<<"->"<<G.vex[k].data<<endl;
}
arc=arc->nextarc;
}
} return;
} int main(void)
{
ALGraph G;
InitALGraph(G); CriticalPath(G); system("pause");
return 0;
}

关键路径(AOE)---《数据结构》严蔚敏的更多相关文章

  1. [数据结构]严蔚敏版(C数据结构)配套实现程序111例

    以下为根据严蔚敏版数据结构的示例和概念实现的程序 目录 一.严蔚敏版(C数据结构)配套实现程序111例 1.数组与字符串 2.栈与队列 3.链表LinkList 4.树与二叉树 5.排序相关算法 6. ...

  2. 静态链表的C实现(基于数据结构 严蔚敏)

    静态链表是利用一维数组实现逻辑上的单链表结构,结点的逻辑上相邻但物理位置上不一定相邻,因为内存分配上是一次性的,故称为静态. 特点: 预先需要一片连续的存储空间: 非随机存取: 无现成的"内 ...

  3. 基于c语言数据结构+严蔚敏——线性表章节源码,利用Codeblocks编译通过

    白天没屌事,那我们就来玩玩线性表的实现吧,快要失业了,没饭吃了咋整哦 题目描述假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员),现要求一个新的集合A=A∪B ...

  4. 《数据结构-C语言版》(严蔚敏,吴伟民版)课本源码+习题集解析使用说明

    <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明 先附上文档归类目录: 课本源码合辑  链接☛☛☛ <数据结构>课本源码合辑 习题集全解析  链接☛☛☛  ...

  5. 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚 ...

  6. 9-9-B+树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - B+树 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题 ...

  7. 9-8-B树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - B树 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集 ...

  8. 7-6-有向图强连通分量的Kosaraju算法-图-第7章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第7章  图 - 有向图强连通分量的Kosaraju算法 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严 ...

  9. 6-11-N皇后问题-树和二叉树-第6章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第6章  树和二叉树 - N皇后问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本 ...

  10. 6-9-哈夫曼树(HuffmanTree)-树和二叉树-第6章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第6章  树和二叉树 - 哈夫曼树(HuffmanTree) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版> ...

随机推荐

  1. 【搜索】P1041 传染病控制

    题目链接:P1041 传染病控制 题解: 这个题目是看别人的博客做出来的,其实挺不错的一个题目,考察的东西挺多的, 一个dfs可以处理5个东西: 1.找出父亲 2.找出深度 3.每一层的节点,存进Ve ...

  2. QT_仅仅直接在构造函数中创建对象的不可行的原因

    #include "mainwidget.h" #include <QApplication> int main(int argc, char *argv[]) { Q ...

  3. luogu P1821 Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  4. 21. SCHEMATA

    21. SCHEMATA 在MySQL中,SCHEMA是数据库,因此SCHEMATA表提供有关数据库的信息. SCHEMATA表有以下列: CATALOG_NAME :SCHEMA所属目录的名称.该值 ...

  5. 如何用 CSS 创作一个立体滑动 toggle 交互控件

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/zjoOgX 可交互视频教程 此视 ...

  6. python_OS 模块

    os模块 用于提供系统级别的操作 os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") # 改变当前脚本工作目 ...

  7. DEV Express中Bar Manager的使用

    未排版 在barManager中可以添加多种元素,如皮肤按钮,复选框等,但是下拉菜单却给出了多个冗余的控件. 遗留问题:怎么设置Bar为大图标,查找是否存在Ribbon控件. Bar 1,       ...

  8. gitlab+jenkins+docker自动构建

    docker容器部署gitlab: sudo docker run --detach \ --hostname git.gitlab.com \ --net=host \ --publish 9443 ...

  9. 配置工程文件dll编译后copy路径

    放到工程文件的最后面的配置节点: 下面的配置节点中生成路径换成实际的相对路径就可以了 修改:Prject.csproj 文件里面的配置节点  project配置节点里面的最后面 <Target ...

  10. Java.lang.NoSuchMethodError: 后带 V/Z等字母的

    知道 Java.lang.NoSuchMethodError: 后带 V/Z等字母的 错误,一般都是 jar包冲突引起的,找到冲突的jar包,去掉一个就好