// 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. jquery 定位

    jquery 定位 <html> <head> <title>jquery 定位</title> </head> <body> ...

  2. 【2019.6.2】python:json操作、函数、集合、random()等

    一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...

  3. 全新Ubentu系统没有make,gcc命令解决办法

    一定要记得先update sudo apt-get update 然后输入下述命令即可 sudo apt-get install make sudo apt-get install gcc

  4. 13. OPTIMIZER_TRACE

    13. OPTIMIZER_TRACE OPTIMIZER_TRACE表提供由跟踪语句的优化程序跟踪功能生成的信息. 要启用跟踪,请使用optimizer_trace系统变量. 有关详细信息,请参阅M ...

  5. Linux磁盘管理及Lvm

    1. 硬盘接口 IDE: SATA:常用: SCSI:主要用于高端服务器,linux默认: SAS 2. 硬盘种类 SATA硬盘:串口硬盘,有较强的纠错能力: SCSI 硬盘:默认硬盘: SAS 硬盘 ...

  6. django下的framework

    可以创建个虚拟环境先,不过我没使用这个方式 virtualenv env source env/bin/activate ------ 退出: To exit the virtualenv envir ...

  7. Python数据可视化库-Matplotlib(二)

    我们接着上次的继续讲解,先讲一个概念,叫子图的概念. 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.a ...

  8. 如何根据实体动态生成sql语句

    该文章同时解决了,如何向数据库中添加Null值,以及如何处理“参数化查询未提供参数”的错误.解决方案请看第二段折叠的代码. 背景: 在项目开发的过程中,往往需要根据实体的值来修改sql语句,比如说,有 ...

  9. luogu1447 [NOI2010]能量采集

    考虑暴力,答案显然是 \(\sum_{i=1}^n\sum_{j=1}^m(2(\gcd(i,j)-1)+1)=\sum_{i=1}^n\sum_{j=1}^m(2\gcd(i,j)-1)\). 考虑 ...

  10. 大数据学习——装私服nexus

    (一)安装 解压之后 进入bin文件夹下 复制路径 D:\software\nexus-2.12.0-01-bundle\nexus-2.12.0-01\bin 进入命令行窗口输入以下命令,安装成功 ...