码迷,mamicode.com
首页 > 其他好文 > 详细

hdoj 2647 Reward

时间:2015-08-25 23:44:40      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:reward   hdoj 2647   拓扑排序   

Reward

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


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
本题题意:老板给员工发奖励,员工有不同的要求,例如 1 2 ;说明1 要求奖励比2高;
如果 出现 环的情况,则输出-1;没有环则输出老板最少需要多少钱发奖励!
所以 1 2情况,则令1的奖励为888,2的奖励为889;
代码如下:
<pre name="code" class="cpp">#include<stdio.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
#include<string.h>
#define  N 11000
#define  INF  0xffff
struct line
{
    int u,v,w;
    int next;
}edge[21000];
int top=0,indegree[N];
int t,n,m,head[N];
void add(int u,int v)
{
    edge[top].v =v;
    edge[top].next =head[u];
    head[u]=top++;
    indegree[v]++;
}
void topo()
{
    int i,j,val=0,x=888,s,v[N],k=0;
     queue<int>Q;
    s=INF;
    for(i=1;i<=n;i++)
     {
         v[i]=888;// 初始化所有人奖励都为888 
         if(indegree[i]==0)//入度为零入队 
         {
             s=i;
            Q.push(i);
         }
     }
     if(s==INF)//当刚开始的没有入度为零的点,输出-1,return  
     {
     printf("-1\n");
     return ;
     }   
     while(!Q.empty() )
     {
         top=Q.front () ;
         Q.pop();
         indegree[top]=-1;
         s=INF;
         val+=v[top];//奖励加起来 
          k++; //标记 入队人数 
         for(i=head[top];i!=-1 ;i=edge[i].next )
         { 
             indegree[edge[i].v ]--;
             if(indegree[edge[i].v ]==0)
             {
                 s=edge[i].v ;
                 Q.push(edge[i].v ); 
             
            }
            v[edge[i].v]=v[top]+1;//与top相连的点奖励加一 
         }
     }
     if(k<n)//判断是否全部入队,即是否有环 
     printf("-1\n");
     else
     {
         printf("%d\n",val);
     }
}
int main()
{
    int i;
        
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(head,-1,sizeof(head));
        memset(indegree,0,sizeof(indegree));
        top=0;
        int a,b;
        while(m--)//反向建边 
        {
            scanf("%d%d",&a,&b);
            add(b,a);
        }
        topo();
    }
}









版权声明:本文为博主原创文章,未经博主允许不得转载。

hdoj 2647 Reward

标签:reward   hdoj 2647   拓扑排序   

原文地址:http://blog.csdn.net/zhangxiaoxiang123/article/details/47987637

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!