四、实验内容(或实验步骤) 关键路径、关键工序及项目的工期 事项的最早开始时间 : ES(1)=0; ES(2)=max{0+1,0+3+6}=9; ES(3)=max{0+3}= 3; ES(4)=max{0+4}= 4; ES(5)=max{1+5,3+6+5,3+4}= 14; ES(6)=max{3+3,3+2}= 6; ES(7)=max{3+2,1+5+3,3+6+5+3,3+4+3}=17; ES(8)=max{3+2+8,1+5+3+8,3+6+5+3+8,3+4+3+8}=25; 事项的最晚开始时间 : LF(v8)=25; LF(v7)=min{25-8}=17 LF(v6)=min{25-2}=23 LF(v5)=min{17-3}=14 LF(v4)=min{17-2,23-2}=15 LF(v3)=min{14-4,23-3,9-6}=3 LF(v2)=min{14-5}=9 LF(v1)=min{3-3,9-1,15-3}=0 项目总工期为:25天,相关时间活动列于表(1)中,关键活动是:B,D,E,J,L 关键路径:1-3-2-5-7-8 ![](https://i-blog.csdnimg.cn/blog_migrate/4630344531cc4d1bfafcb8592e2e62ab.png) 五、实验小结(实验结果、存在的问题及解决的方法等) 演示: ![](https://i-blog.csdnimg.cn/blog_migrate/c9ac55343bb49f9963618a7edf6d8a04.png) 关键路径代码: #include <iostream>
using namespace std;
#define max 1000000
int d[10][10];
int earliest[10];
int latest[10];
int path[10];
void findEarliest(int k, int last);
void findLatest(int k, int first);
bool flag = false; //用于判断是否存在关键路径
int main()
{
int i, j, k, m, n = 10;
int u, v, w;
int first, last, count = 0;
int next;
cout << "请输入边数:";
cin >> m;
for (i = 0; i < m; i++)
{
for (j = 0; j < m; j++)
d[i][j] = max;
}
cout << "请输入每条边的顶点及其权重" << endl;
for (i = 0; i < m; i++)
{
cin >> u >> v >> w;
d[u][v] = w;
}
cout << "请输入要查询的两点:";
cin >> first >> last;
for (i = 0; i < 10; i++)
{
earliest[i] = 0;
latest[i] = max;
path[i] = max;
}
k = first;
path[0] = k;
count = 1;
findEarliest(k, last);
if (!flag)
{
cout << "路径不存在";
return 0;
}
k = last;
latest[k] = earliest[k];
findLatest(k, first);
k = first;
while (k != last)
{
for (i = 0; i < 10; i++)
{
if (d[k][i] != max && (latest[i] - d[k][i] == earliest[k]))
{
path[count++] = i;
k = i;
break;
}
}
}
cout << "关键路径为:" << endl;
for (i = 0; path[i] != last; i++)
{
cout << path[i] << "->";
}
cout << path[i];
}
void findEarliest(int k, int last)
{
if (k == last)
return;
flag = false;
for (int i = 0; i < 10; i++)
{
if (d[k][i] < max)
{
flag = true;
if ((earliest[k] + d[k][i]) > earliest[i])
earliest[i] = earliest[k] + d[k][i];
findEarliest(i, last);
}
}
//如果flag没有在循环中被修改为ture,则说明没有节点与k相连(即没有进入if语句内,函数返回不再进行递归),然后在主函数中判断flag的值来决定是否继续
}
void findLatest(int k, int first)
{
if (k == first)
return;
for (int i = 0; i < 10; i++)
{
if (d[i][k] < max)
{
if (latest[k] - d[i][k] < latest[i])
latest[i] = latest[k] - d[i][k];
findLatest(i, first);
}
}
} |