SSLOJ 1325 08年东莞特长生 奖金

Description

由于无敌的凡凡在2005年世界英俊帅气男总决选中胜出,Yali Company总经理Mr.Z心情好,决定给每位员工发奖金。公司决定以每个人本年在公司的贡献为标准来计算他们得到奖金的多少。
  于是Mr.Z下令召开m方会谈。每位参加会谈的代表提出了自己的意见:“我认为员工a的奖金应该比b高!”Mr.Z决定要找出一种奖金方案,满足各位代表的意见,且同时使得总奖金数最少。每位员工奖金最少为100元。

Input

两个整数n,m,表示员工总数和代表数;
以下m行,每行2个整数a,b,表示某个代表认为第a号员工奖金应该比第b号员工高。

Output

若无法找到合法方案,则输出“-1”;否则输出一个数表示最少总奖金。

Sample Input

2 1
1 2

Sample Output

201

思路

topsort(要去环)
去环:
首先,我们如果所有的点都在环里,显然无法topsort,输出-1.
其次,在排除掉第一种情况后,我们一定可以对部分点topsort,但是如果最后有一部分没有topsort,显然一定有环,输出-1.
code:

#include<iostream>
#include<queue>
using namespace std;
int n,m,head[10001],tot=1,rd[10001];
int book[10001];
int x,y;
struct f{
    
    
	int to,next;
} a[20001];
void add(int x,int y)
{
    
    
	a[tot].to=y;
	a[tot].next=head[x];
	head[x]=tot++;
	return;
}
queue<int> wj;
int main()
{
    
    
	cin>>n>>m;
	for (int i=0;i<m;i++)
	{
    
    
		cin>>y>>x;
		add(x,y);
		rd[y]++;
	}
	int o=0;
	for (int j=1;j<=n;j++)
	{
    
    
		if (!rd[j])
		{
    
    
			o++;
			wj.push(j);
		}
	}
	if (o==0)
	{
    
    
		cout<<-1;
		return 0;
	}
	while (wj.size())
	{
    
    
		x=wj.front();
		wj.pop();
		for (int i=head[x];i;i=a[i].next)
		{
    
    
			rd[a[i].to]--;
			if (rd[a[i].to]==0)
			{
    
    
				o++;
				wj.push(a[i].to);
				book[a[i].to]=book[x]+1;
			}
		}
	}
	int s=0;
	for (int i=1;i<=n;i++)
	{
    
    
		if (rd[i]!=0)
		{
    
    
			s=-1;
			break;
		}
		s+=100+book[i];
	}
	cout<<s;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49843717/article/details/112382677