HDU 2647 Reward

版权声明:如果转载,请注明出处。 https://blog.csdn.net/S_999999/article/details/82688704

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13392    Accepted Submission(s): 4281


 

Problem Description

Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.

 

Input

One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.

 

Output

For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.

 

Sample Input

 

2 1

1 2

2 2

1 2

2 1

 

Sample Output

 

1777

-1

#include<stdio.h>
#include<string.h>
 struct EDGE{
	int to;     
	int next;   
	int money;  
} eg[200010];
int n,t,into[10009];
int head[10009];  
int max( int a, int b){
	return a > b ? a : b; 
}
void add( int a ,int b){
	eg[t].to = a;
	eg[t].next = head[b];
	head[b] = t++;
	 
}

int topo(){
	int i,tmp,j;
	for( i =1;i<=n;i++){
		int k = 1;
		while(into[k]!=0)
		 k++;
		 if( k > n)
		  return -1;
    
		 into[k]--;       
		 for( j = head[k];j!=-1; j = eg[j].next){
		 	int tmp = eg[j].to;
		 	into[tmp]--;
		 	eg[tmp].money = max( eg[k].money +1,eg[tmp].money);
		 }

		
	}
	return 0;
}


int main(){
	
	int m,i,j,s,a,b;
	while(~scanf("%d%d",&n,&m)){
	 
	    memset(eg,0,sizeof(eg));
	    memset(into,0,sizeof(into));
	    memset(head,-1,sizeof(head));
	    t = 0;
	    for( i =0; i<m;i++)
	    {
	    	scanf("%d%d",&a,&b);
	    	add(a,b);
	    	into[a]++;
		}
	    s = topo();
	    if( s== -1)
	       printf("-1\n");
	    else {
	       
	       for( i =1;i<=n;i++)
		    s+=eg[i].money;
			   
		   printf("%d\n",s+888*n);
		 }
	} 
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/S_999999/article/details/82688704