关键路径(AOE)---《数据结构》严蔚敏
// 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)---《数据结构》严蔚敏的更多相关文章
- [数据结构]严蔚敏版(C数据结构)配套实现程序111例
以下为根据严蔚敏版数据结构的示例和概念实现的程序 目录 一.严蔚敏版(C数据结构)配套实现程序111例 1.数组与字符串 2.栈与队列 3.链表LinkList 4.树与二叉树 5.排序相关算法 6. ...
- 静态链表的C实现(基于数据结构 严蔚敏)
静态链表是利用一维数组实现逻辑上的单链表结构,结点的逻辑上相邻但物理位置上不一定相邻,因为内存分配上是一次性的,故称为静态. 特点: 预先需要一片连续的存储空间: 非随机存取: 无现成的"内 ...
- 基于c语言数据结构+严蔚敏——线性表章节源码,利用Codeblocks编译通过
白天没屌事,那我们就来玩玩线性表的实现吧,快要失业了,没饭吃了咋整哦 题目描述假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员),现要求一个新的集合A=A∪B ...
- 《数据结构-C语言版》(严蔚敏,吴伟民版)课本源码+习题集解析使用说明
<数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明 先附上文档归类目录: 课本源码合辑 链接☛☛☛ <数据结构>课本源码合辑 习题集全解析 链接☛☛☛ ...
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- 9-9-B+树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - B+树 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题 ...
- 9-8-B树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - B树 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集 ...
- 7-6-有向图强连通分量的Kosaraju算法-图-第7章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第7章 图 - 有向图强连通分量的Kosaraju算法 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严 ...
- 6-11-N皇后问题-树和二叉树-第6章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第6章 树和二叉树 - N皇后问题 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本 ...
- 6-9-哈夫曼树(HuffmanTree)-树和二叉树-第6章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第6章 树和二叉树 - 哈夫曼树(HuffmanTree) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版> ...
随机推荐
- 【2019.6.2】python:json操作、函数、集合、random()等
一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...
- 深入理解Spring IoC容器和动态代理机制
Deployment期间验证 实现一: <bean id="theTargetBean" class="..."/> <bean id=&qu ...
- Linux vim指令学习
每天查看一遍vim文档 Linux系统下命令:$ vimtutor 1.可视模式([v] 或者 [Ctrl + v])下的[U]把选中的文本变为大写 .[u]把选中的文本变为小写. 2.[数字] + ...
- go语言碎片整理之标准库log
log Go语言内置的log包实现了简单的日志服务.本文介绍了标准库log的基本使用. 使用Logger log包定义了Logger类型,该类型提供了一些格式化输出的方法.本包也提供了一个预定义的“标 ...
- tomcat官网改版后下载方式
位于disk/tomcat目录下 http://archive.apache.org/dist/tomcat/ 具体例如:http://archive.apache.org/dist/tomcat/t ...
- shiro框架的四中权限控制方式
https://www.cnblogs.com/cocosili/p/7103025.html 一.在自定义的realm中进行权限控制 在applicationContext.xml文件中添加 /a ...
- python基础——2(基本数据类型及运算符)
目录 为何数据要区分类型? 一.数字类型 1.整型int 2.浮点型float 二.字符串str 三.列表类型list 四.字典类型 五.布尔类型 运算符的介绍 一.算术运算符 二.比较运算符 三.赋 ...
- ES6(Proxy 和 Reflect)
Proxy 和 Reflect 1.Proxy 和 Reflect 的概念 Proxy 意为 ‘代理’,连接了用户和真实对象之间的一个层 Reflect 意为‘反射’ 反射的是Object 2.适 ...
- zoj 2857 Image Transformation
Image Transformation Time Limit: 2 Seconds Memory Limit: 65536 KB The image stored on a compute ...
- 【Java 理论篇 1】Java2平台的三个版本介绍
导读:关于java的三种分类J2SE.J2EE.J2ME,在网上有很多资料,然后自己写的,也大多是从各个网站上搜罗里的.算是自己的一种笔记,或者明白的说,就是把别人的东西抄一遍.但是,这对于我来说,也 ...