zoukankan      html  css  js  c++  java
  • Reward

    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
    一个老板给员工发红包    每个人最少888元  按照荣誉高低来发放红包  但每个人的钱都不能一样     例子的第一个:1的钱要比2多  但老板又想用最少的钱  于是就多给1  1元也就是889元  
     当有矛盾的时候输出-1  比如例子的第二个:1比2多  2比1多  这就矛盾了
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<queue>
    using namespace std;
    int n,m;
    int map[1001][1001];
    int die[100001];
    int val[100001];
    int head[100001];
    struct node 
    {
    	int to,next;
    }ren[200001];
    void topo()
    { 
      
           queue<int>q;
           for(int i=1;i<=n;i++)
           {
           	  val[i]=888;
           	   if(die[i]==0)
           	   q.push(i);
    	   }
    	   int ans=0;  int sum=0;
    	   while(!q.empty())
    	   {
    	   	ans++;
    	   	int first=q.front();
    	   	die[first]=-1;
    	   	sum+=val[first];
    	   	q.pop();
    	   		for(int j=head[first];j!=-1;j=ren[j].next )
    		{
    	      die[ren[j].to]--;
    	      if(die[ren[j].to ]==0)
    	      {
    	      	q.push(ren[j].to);
    		  }
    		  val[ren[j].to ]=val[first]+1;
    		}
    	   }
    	   if(ans==n)
    	   {
    	   	printf("%d
    ",sum);
    	   }
    	   else
    	   {
    	   	printf("-1
    ");
    	   }
    }
    int main()
    {     
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    			memset(head,-1,sizeof(head));
    	       memset(die,0,sizeof(die));
    		int x,y;
    	  for(int i=0;i<m;i++)
    	  {
    	  		scanf("%d%d",&x,&y);
    			ren[i].to =x;
    			ren[i].next =head[y];
    			head[y]=i; 
    			   die[x]++;
    	  }
    		topo();
    	}
    	return 0;
    }


  • 相关阅读:
    相邻相邻问题
    修改sqlserver2008数据库的排序规则 (转)
    备份删除
    SQL Server 2005系统数据库master重建、修复(转)
    SQL SERVER 2008 重建损坏的master (转)
    深入理解JavaScript作用域和作用域链
    js词法分析
    利用 Console 来学习、调试JavaScript
    expression解决IE6下固定定位的兼容
    浏览器事件的思考
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027084.html
Copyright © 2011-2022 走看看