用C语言写一个求解关键路径函数

时间: 2023-12-29 13:49:10 浏览: 31
由于关键路径算法较为复杂,需要使用图论中的拓扑排序和动态规划的思想,因此代码较长。以下是一个使用C语言实现的求解关键路径的函数: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NODE 1000 #define MAX_EDGE 10000 #define INF 0x3f3f3f3f typedef struct Edge { int from, to, weight; } Edge; int node_num, edge_num; int head[MAX_NODE], nxt[MAX_EDGE], to[MAX_EDGE], weight[MAX_EDGE], cnt; int in_degree[MAX_NODE], out_degree[MAX_NODE], earliest[MAX_NODE], latest[MAX_NODE]; int queue[MAX_NODE], front, rear; void add_edge(int u, int v, int w) { nxt[++cnt] = head[u]; to[cnt] = v; weight[cnt] = w; head[u] = cnt; } void topo_sort() { front = 0, rear = -1; for (int i = 1; i <= node_num; i++) if (in_degree[i] == 0) queue[++rear] = i; while (front <= rear) { int u = queue[front++]; for (int i = head[u]; i; i = nxt[i]) { int v = to[i], w = weight[i]; in_degree[v]--; if (in_degree[v] == 0) queue[++rear] = v; earliest[v] = earliest[u] + w; } } } int critical_path() { memset(in_degree, 0, sizeof(in_degree)); memset(out_degree, 0, sizeof(out_degree)); memset(earliest, 0, sizeof(earliest)); memset(latest, INF, sizeof(latest)); memset(head, 0, sizeof(head)); memset(nxt, 0, sizeof(nxt)); memset(to, 0, sizeof(to)); memset(weight, 0, sizeof(weight)); cnt = 0; scanf("%d %d", &node_num, &edge_num); for (int i = 0; i < edge_num; i++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); add_edge(u, v, w); in_degree[v]++; out_degree[u]++; } topo_sort(); int end_node = 1; for (int i = 2; i <= node_num; i++) if (earliest[i] > earliest[end_node]) end_node = i; latest[end_node] = earliest[end_node]; rear = -1; for (int i = 1; i <= node_num; i++) if (out_degree[i] == 0) queue[++rear] = i; while (front <= rear) { int u = queue[front++]; for (int i = head[u]; i; i = nxt[i]) { int v = to[i], w = weight[i]; out_degree[u]--; if (out_degree[u] == 0) queue[++rear] = u; if (latest[v] - w < latest[u]) latest[u] = latest[v] - w; } } int ans = 0; for (int i = 1; i <= node_num; i++) for (int j = head[i]; j; j = nxt[j]) { int u = i, v = to[j], w = weight[j]; if (earliest[u] + w == earliest[v] && latest[v] - w == latest[u]) if (ans < earliest[u] + w) ans = earliest[u] + w; } return ans; } int main() { printf("%d\n", critical_path()); return 0; } ``` 函数中主要使用了拓扑排序和动态规划的思想来求解关键路径,具体实现如下: 1. 定义了一个结构体`Edge`来存储边的起点、终点和权重。 2. 声明了一些全局变量,包括节点数、边数、邻接表、入度、出度、最早开始时间和最晚开始时间等。 3. 实现了添加边的函数`add_edge`,用于向邻接表中加入一条边。 4. 实现了拓扑排序的函数`topo_sort`,用于求出每个节点的最早开始时间。 5. 实现了求解关键路径的函数`critical_path`,在函数中使用了拓扑排序求出每个节点的最早开始时间和最晚开始时间,从而求出关键路径和关键路径长度。 6. 在`main`函数中调用`critical_path`函数并输出结果。 值得注意的是,函数中使用了队列来实现拓扑排序,其中用`front`和`rear`来表示队列的头和尾,用`queue`数组来存储队列中的元素。同时,为了方便起见,代码中使用了邻接表来存储图的信息。

相关推荐

最新推荐

recommend-type

C语言求解无向图顶点之间的所有最短路径

主要为大家详细介绍了C语言求解无向图顶点之间的所有最短路径,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

用C语言求幂函数和指数函数的方法

主要介绍了用C语言求幂函数和指数函数的方法,即pow()函数和sqrt()函数的使用,需要的朋友可以参考下
recommend-type

c语言文件操作常用函数及读写文件代码举列

文章主要例举一个c语言文件操作常用函数及读写文件的代码,感性趣的朋友可以看看。
recommend-type

C语言参数个数可变函数详解

exec*()系列函数,用于在程序中执行外部文件(main(int argc,char*argv[])算不算呢,与其说main()也是一个可变参数函数,倒不如说它是exec*()经过封装后的具备特殊功能和意义的函数,至少在原理这一级上有很多相似之...
recommend-type

C语言中free函数的使用详解

free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。