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;
    }


  • 相关阅读:
    Android Camera 使用小结
    Android 调用堆栈跟踪
    读取文件
    Android USB大容量存储时SD卡状态监听(转)
    Setting up PhoneGap on Ubuntu for Android app development
    python单元测试
    python数据库备份
    python多线程多进程
    python面向对象编程和类
    python异常处理及Url编码
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027084.html
Copyright © 2011-2022 走看看