关键活动相关求解

// 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值