// 4.44.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<queue>
#define N 10
using namespace::std;
void Forward(int VE[N],int matrix[N][N],int temp)
{
for (int opt = 0; opt < N; opt++)
{
if (((matrix[opt][temp] + VE[opt]) > VE[temp]) && (matrix[opt][temp] > 0))
VE[opt] = VE[temp] + matrix[temp][opt];
}
}
void Backward(int VL[N], int matrix[N][N], int temp)
{
for (int opt = 0; opt < N; opt++)
{
if (((-matrix[temp][opt] + VL[opt]) > VL[temp]) && (matrix[temp][opt] > 0))
VL[temp] = VL[opt] - matrix[temp][opt];
}
}
int main()
{
int matrix[N][N];
int weight = -1;
int start[N];
int des[N];
int count = 0;//记录活动数
int mark[10];//记录入度
int VE[N];//记录事件最早开始时间
int VL[N];//记录事件最迟开始时间
int E[N];//记录活动最早开始时间
int L[N];//记录活动最迟发生时间
int delay[N];//活动推迟的时间
int i,j;
//初始化邻接矩阵
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
matrix[i][j] = -1;
}
}
//读入数据
for (int opt = 0; opt < N; opt++)
{
cout << "Input start destination and weight:" << endl;
cin >> i >> j >> weight;
matrix[i][j] = weight;
if (weight > 0)
{
start[count] = i;
des[count] = j;
count++;
}
mark[j]++;
}
queue<int> q;
for (int j = 0; j < 10; j++)
{
if (mark[j] == 0)
q.push(j);
}
while (!q.empty())
{
int temp=q.front();
q.pop();
Forward(VE,matrix,temp);
Backward(VL, matrix, temp);
for (int opt = 0; opt < N; opt++)
{
if (matrix[temp][opt] > 0)
{
mark[opt]--;
if (mark[opt] == 0)
{
q.push(opt);
}
}
}
}
for (int i = 0; i < count; i++)
{
E[i] = VE[start[i]];
}
for (int i = 0; i < count; i++)
{
L[i] = VL[des[i]] - matrix[start[i]][des[i]];
}
for (int opt = 0; opt < N; opt++)
{
delay[opt] = L[opt] - E[opt];
if (delay[opt] == 0)
cout << start[opt] << "->" << des[opt] << endl;
}
return 0;
}
关键活动相关求解
最新推荐文章于 2023-07-10 00:16:15 发布