关键活动相关求解

来源:互联网 发布:自制搞笑视频软件 编辑:程序博客网 时间:2024/05/21 22:43
// 4.44.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<queue>#define N 10using 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;}

原创粉丝点击