[SSL] 1325 bonus (topological sort)

[SSL] 1325 bonus (topological sort)

Time Limit:1000MS
Memory Limit:65536K

Description

As the invincible Fanfan won the 2005 World Handsome and Handsome Male Finals, the general manager of Yali Company, Mr.Z, was in a good mood and decided to give bonuses to each employee. The company decided to calculate the amount of bonus they received based on each person's contribution to the company this year.
  So Mr.Z ordered the m-party talks. Each representative who participated in the meeting put forward his own opinion: "I think the bonus of employee a should be higher than b!" Mr.Z decided to find a bonus plan to meet the opinions of the representatives and at the same time minimize the total bonus . The minimum bonus for each employee is 100 yuan.

Input

Two integers, n, m, represent the total number of employees and the number of representatives; the
following m lines, each line contains 2 integers a, b, indicating that a representative believes that the bonus of employee a should be higher than employee b.

Output

If no legal solution can be found, output "-1"; otherwise, output a number to indicate the minimum total bonus.

Sample Input

2 1
1 2

Sample Output

201

Hint

80% of the data meets n<=1000, m<=2000;
100% of the data meets n<=10000, m<=20000.

Ideas

Do topological sorting
The algorithm is described as follows:
1. Initialize an int[] inDegree to save the in-degree of each node.
2. For the child nodes of each node in the graph, add 1 to the in-degree of its child nodes.
3. Select a node with an in-degree of 0 to start traversal, and add this node to the output.
4. For each node that has been traversed, update the in-degree of its child node: subtract 1 from the in-degree of the child node.
6. Repeat step 3 until all nodes are traversed.
If it is not possible to traverse all the nodes, it means that the current graph is not a directed acyclic graph. There is no topological sort.
If and only if a directed graph is a directed acyclic graph (directed acyclic graph, or DAG), the topological order corresponding to the graph can be obtained. Every directed acyclic graph has at least one topological sort.

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];
queue<int> q;
struct jgt
{
    
    
	int x,y,nxt;
}f[200010];
void input()
{
    
    
	int i;
	memset(ins,0,sizeof(ins));
	memset(ans,0,sizeof(ans));
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;i++)
	{
    
    
		tot++;
		scanf("%d%d",&f[tot].y,&f[tot].x);
		f[tot].nxt=head[f[tot].x];
		head[f[tot].x]=tot;
		ins[f[tot].y]++;
	}
	return;
}
void topsort()
{
    
    
	int i;
	for(i=1;i<=n;i++)
		if(!ins[i])
			q.push(i);
	for(;!q.empty();q.pop(),sum++)
	{
    
    
		for(i=head[q.front()];i;i=f[i].nxt)//更新入度 
		{
    
    
			ins[f[i].y]--;
			if(!ins[f[i].y])//当能够到达这个点的所有点都遍历过后将这个点加入队列
			{
    
    
				ans[f[i].y]=ans[q.front()]+1;
				q.push(f[i].y);
			}
		}
	}
	return;
}
void output()
{
    
    
	int i,answer=n*100;
	if(sum<n)//循环依赖 
	{
    
    
		printf("-1");
		return;
	}
	for(i=1;i<=n;i++)
		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/112394203