// 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. MyBatis的动态sql小练习,小回顾

    关键字if+trim trim可以去除多余的关键字,是where和set的组合 trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: <trim prefix=& ...

  2. c++_核桃的数量

    #include <iostream> using namespace std; int gcd(int x,int y){ int temp; ){ temp=x%y; x=y; y=t ...

  3. 【HIHOCODER 1320】压缩字符串(区间DP)

    描述 小Hi希望压缩一个只包含大写字母'A'-'Z'的字符串.他使用的方法是:如果某个子串 S 连续出现了 X 次,就用'X(S)'来表示.例如AAAAAAAAAABABABCCD可以用10(A)2( ...

  4. 数据库连接 Mysqli

    //数据库连接 Mysqli $db= new Mysqli("localhost","root","root","asd_808 ...

  5. HDU-1210Eddy's 洗牌问题

    Eddy's 洗牌问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Prob ...

  6. 搜狗大数据总监、Polarr 联合创始人关于深度学习的分享交流 | 架构师小组交流会

    架构师小组交流会是由国内知名公司技术专家参与的技术交流会,每期选择一个时下最热门的技术话题进行实践经验分享.第一期:来自沪江.滴滴.蘑菇街.扇贝架构师的 Docker 实践分享 第二期:来自滴滴.微博 ...

  7. hihoCoder#1133 二分·二分查找之k小数

    原题地址 经典问题了,O(n)时间内找第k大的数 代码: #include <iostream> using namespace std; int N, K; int *a; int se ...

  8. [HDU3065]病毒持续侵袭中(AC自动机)

    传送门 AC自动机的又一模板,统计每个字符串在文本中的次数. 所以就不需要vis数组了. ——代码 #include <cstdio> #include <cstring> # ...

  9. 【HDOJ5640】King's Cake(数论)

    题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...

  10. 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制

    通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制 前言说明 本篇为网易云课堂Linux内核分析课程的第四周作业,我将通过调用C语言的库函数与在C代码中 ...