(CBT) Critical Path

Critical Path

description:

Calculating the length of the critical path AOE- network.

Input:

       The first line of the input data is a positive integer, n represents the number of vertices in the drawing (vertices respectively 0,1, ..., n-1 for numbering), the number of vertices does not exceed 100, where 0 is the source point, N- 1 Meeting point. n lines after each line containing n integers, the adjacency matrix is ​​AOE- network, wherein the non-directly reachable arc between two vertices represent 0, an integer greater than zero indicates the duration of activity.

Output:

AOE- critical length of the output network path, if the ring network, the output "NO".

 

Example input

9

0 6 4 5 0 0 0 0 0

0 0 0 0 1 0 0 0 0

0 0 0 0 1 0 0 0 0

0 0 0 0 0 2 0 0 0

0 0 0 0 0 0 9 7 0

0 0 0 0 0 0 0 4 0

0 0 0 0 0 0 0 0 2

0 0 0 0 0 0 0 0 4

0 0 0 0 0 0 0 0 0

Sample output

18

 

 

Output: If there is a ring network, the sample output:

NO

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int i, j, n, count;
	int G[100][100];
	int indegree[100] = { 0 };
	int stack[100], top = -1;
	int ve[100];
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			scanf("%d", &G[i][j]);
			if(G[i][j])
				indegree[j]++;
		}
	}
	for (i = 0; i < n; i++)
	{
		if (!indegree[i])
			stack[++top] = i;
	}
	count = 0;
	while (top > -1)
	{
		i = stack[top--];
		count++;
		for (j = 0; j < n; j++)
		{
			if (G[i][j])
			{
				indegree[j]--;
				if (!indegree[j])
				{
					stack[++top] = j;
					if (ve[j] < G[i][j] + ve[i])
						ve[j] = G[i][j] + ve[i];
				}
			}
		}
	}
	if (count < n)
		printf("NO\n");
	else
		printf("%d", ve[n - 1]);
	return 0;
 }

  

Guess you like

Origin www.cnblogs.com/KIROsola/p/11909433.html