[SSL] Project 2876 (topological sorting)

[SSL] Project 2876 (topological sorting)

Time Limit:1000MS
Memory Limit:256000K

Description

Zhang San is a project engineer for an engineering company. One day the company took on a large-scale project. Before the construction of the large-scale project, the company first divided the entire project into several sub-projects and numbered these sub-projects as 1, 2,..., N; after this division, the sub-projects There will be some dependencies, that is, some sub-projects must be completed after some sub-projects can be constructed, the company needs engineer Zhang San to calculate the minimum completion time of the entire project.
Regarding the above problems, you can assume:
1. According to the budget, each sub-project has a completion time.
2. The dependency between sub-projects is: some sub-projects must be started after some sub-projects are completed.
3. As long as the dependency between the sub-projects is satisfied, any number of sub-projects can be under construction at any time, that is, the number of sub-projects under simultaneous construction is not limited.
For example: there are five sub-projects project planning table:

Insert picture description here

Now for the given sub-project planning situation and the time required to complete each sub-project, if the sub-projects are divided reasonably, the minimum time required to complete the entire project is calculated, and if the sub-projects are unreasonably divided, output -1.

Input

The first line is a positive integer N, which represents the number of sub-projects (N<=200),
the second line is N positive integers, which represent the completion time of sub-projects 1, 2, ..., N respectively.
Lines 3 to N+2, each line has N-1 0s or 1, and these 0s or 1s in line K+2 represent "subproject K" and subprojects 1, 2,..., K, respectively -1, K+1,..., N dependence (K=1, 2,..., N). Each row of data is separated by a space.

Output

If the division of sub-projects is reasonable, output the minimum time required to complete the entire project; if the division of sub-projects is unreasonable, output -1.

Sample Input

project.in

5
5 4 12 7 2
0 0 0 0
0 0 0 0
0 0 0 0
1 1 0 0
1 1 1 1

project.in

5
5 4 12 7 2
0 1 0 0
0 0 0 0
0 0 1 0
1 1 0 0
1 1 1 1

Sample Output

project.out

14

project.out

-1

Ideas

Topological sorting, using an array to save the time to complete each point (including dependent points).
Find the maximum value.

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int ans[100010],sum=0,tot=0,n,m,head[100010],ins[100010],times[210];
queue<int> q;
struct jgt
{
    
    
	int x,y,nxt;
}f[200010];
void input()
{
    
    
	int i,j,t;
	memset(ins,0,sizeof(ins));
	memset(ans,0,sizeof(ans));
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&times[i]);
	for(i=1;i<=n;i++)
	{
    
    
		for(j=1;j<=n;j++)
		{
    
    
			if(i!=j)
			{
    
    
				scanf("%d",&t);
				if(t)
				{
    
    	
					tot++;
					f[tot].x=j;
					f[tot].y=i;
					f[tot].nxt=head[j];
					head[j]=tot;
					ins[i]++;
				}
			}
		}
	}
	return;
}
void topsort()
{
    
    
	int i;
	for(i=1;i<=n;i++)
		if(!ins[i])
		{
    
    
			q.push(i);
			ans[i]=times[i];
		}
	for(;!q.empty();q.pop(),sum++)
	{
    
    
		for(i=head[q.front()];i;i=f[i].nxt)//更新入度 
		{
    
    
			ins[f[i].y]--;
			ans[f[i].y]=max(ans[f[i].y],ans[q.front()]+times[f[i].y]);
			if(!ins[f[i].y])//当能够到达这个点的所有点都遍历过后将这个点加入队列
				q.push(f[i].y);
		}
	}
	return;
}
void output()
{
    
    
	int i,answer=0;
	if(sum<n)//循环依赖 
	{
    
    
		printf("-1");
		return;
	}
	for(i=1;i<=n;i++)//找最大值
		answer=max(answer,ans[i]);
	printf("%d",answer);
	return;
}
int main()
{
    
    
	input();
	topsort();
	output();
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/112875447