2008 Dongguan Special Longevity Bonus

2008 Dongguan Special Longevity Bonus

Description
  Since 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 scheme 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, representing the total number of employees and the number of representatives; the
following m lines, each line with 2 integers a, b, indicate that a representative believes that the bonus of employee a should be higher than employee b.

Output
If you can not find a legitimate program, output "-1"; otherwise, the output number represents a minimum total prize money.

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.

Idea:
We sort each relationship topologically, and then we set f[i] to represent the wages of the i-th worker. Which level it is in the topology, the wages is equal to =100+level. Finally, for 1...n to, add all the wages of n workers to Euros!

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e6;
struct node
{
    
    
	ll u,v,next;
} edge[N];
ll n,m,in[N],que[N],head,tail,f[N],ans,tot,hd[N];
bool vis[N];
void add(ll x,ll y) {
    
    edge[++tot]=(node){
    
    x,y,hd[x]};hd[x]=tot;}
void topsort()
{
    
    
	for(ll i=1;i<=n;i++) if(!in[i]) que[++tail]=i,vis[i]=1,f[i]=100;
	while(head<tail)
	{
    
    
		head++;
		int x=que[head];
		for(ll i=hd[x];i;i=edge[i].next)
		{
    
    
			ll y=edge[i].v;
			if(!vis[y])
			{
    
    
				in[y]--;
				if(!in[y]) que[++tail]=y,vis[y]=1,f[y]=f[x]+1;	
			}
		} 
	}
}
int main()
{
    
    
	scanf("%lld%lld",&n,&m);
	for(ll i=1;i<=m;i++)
 	{
    
    
	 	ll u,v;
		scanf("%lld%lld",&u,&v);
		add(v,u),in[u]++;
	}
	topsort();
	for(int i=1;i<=n;i++) 
	{
    
    
		if(in[i]) {
    
    printf("-1");return 0;}
		else ans+=f[i];
	} 
	printf("%lld",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/108077184