【SSL_P1325】08年东莞特长生 奖金

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

Hint

80%的数据满足n<=1000,m<=2000;
100%的数据满足n<=10000,m<=20000。

解题思路

BFS+拓扑

#include<iostream>
#include<cstdio>
using namespace std;

int n,m,a[100010],b[100010];
int tot,head[100010],f[100010];
int ans,t;

struct abc{
    
    
	int to,next;
}s[200010];

void add(int x,int y)
{
    
    
	s[++tot]=(abc){
    
    y,head[x]};
	head[x]=tot;
}

void bfs()
{
    
    
	int tl=0,hd=0;
	for(int i=1;i<=n;i++)
		if(b[i]==0)
		{
    
    
			tl++;
			f[tl]=i;
			a[i]=0;
		}
	if(tl==0)
	{
    
    
		cout<<-1<<endl;
		t=1;
		return;
	}
	while(hd<tl)
	{
    
    
		hd++;
		if(f[hd]==0) continue;
		for(int i=head[f[hd]];i;i=s[i].next)
		{
    
    
			b[s[i].to]--;
			if(b[s[i].to]==0)
			{
    
    
				tl++;
				f[tl]=s[i].to;
				a[f[tl]]=a[f[hd]]+1;
			}
		} 
	}
}

int main()
{
    
    
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
    
    
		int x,y;
		scanf("%d%d",&y,&x);
		add(x,y);
		b[y]++;
	}
	bfs();
	if(t)
		return 0;
	for(int i=1;i<=n;i++)
	{
    
    
		if(b[i]!=0)
		{
    
    
			ans=-1;
			break;
		}
		ans+=100+a[i];
	}
	cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/SSL_guyixin/article/details/108042733