关键路径(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语言版> ...
随机推荐
- HTML基础(四)表格
定义和用法 <table> 标签定义 HTML 表格. 简单的 HTML 表格由 table 元素以及一个或多个 tr.th 或 td 元素组成. tr 元素定义表格行,th 元素定义表头 ...
- 【亲测可行】Dev c++调试、运行报错解决方法总结
一.编译后 0错误 0警告,但是开始出现‘‘停止运行’’或者进行输入时出现‘‘停止运行’’ 可能的原因: 结构体指针为空,但调用了其成员. 有些scanf语句中忘记添加取址符. 无法跳出递归. 二. ...
- luogu P1238 走迷宫--DFS模板好(水)题
题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数据来描述的,分别表示 ...
- Python旅途——文件操作
Python--文件操作 1.理解文件操作 可能有的时候有人会在想为什么要对文件进行操作,无论对于文件还是网络之间的交互,我们都是建立在数据之上的,如果我们没有数据,那么很多的事情也就不能够成立,比如 ...
- oracle如何重做日志组
1.查询数据库中的重做日志组 SQL> select * from v$log; GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC STA ...
- 安装完Centos 7后的一些处理
1.安装dkms:dkms-2.2.0.3-31.1.noarch.rpm 2.安装显卡驱动:amdgpu-pro-18.10-572953 3.启动图形界面使用init 5 不能使用startx
- sql中Distinct&Count的用法
Distinct作用:消除重复的数值 1.如: select id from T1 select distinct id from T1 二者的检索效果如下: distinct可以用来修饰多列,如: ...
- SpringMVC修改功能
articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...
- HDU 5468 Puzzled Elena
Puzzled Elena Time Limit: 2500ms Memory Limit: 131072KB This problem will be judged on HDU. Original ...
- zoj 2001 Adding Reversed Numbers
Adding Reversed Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB The Antique Comedians of M ...