HDU 2647 Reward

Reward

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

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
 
Author
dandelion
 
Source
 
Recommend
yifenfei
 
提供一组数据
7 7
7 6
7 5
7 1
6 5
5 4
4 3
3 1
 
6231
 
思路:拓扑排序的特殊处理;因为同一等级的工人的奖金是一样
 
这样的话,1和3的奖金都是888,而2应该是889,4是890
 
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m;
int first,end;
int p;
int sum[100010];
int head[100010];
int inin[100010];
int indegree[100010];
int the_last_ans;
int the_last_sum;
struct EdgeNode
{
    int to;
    int next;
};
EdgeNode Edges[100010];
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(sum,0,sizeof(sum));
        memset(Edges,0,sizeof(Edges));
        memset(head,0,sizeof(head));
        memset(indegree,0,sizeof(indegree));
        memset(inin,0,sizeof(inin));
        the_last_ans = 0;
        the_last_sum = 0;
        for(int i = 1;i <= m;i ++)
        {
            scanf("%d%d",&end,&first);
            if(end == first)
               continue ;
            for(int j = head[first];j != 0;j = Edges[j].next)
            {
                if(Edges[j].to == end)
                    continue ;
            }
            indegree[end] ++;
            inin[end] ++;
            Edges[i].to = end;
            Edges[i].next = head[first];
            head[first] = i;
        }
        int qqueue[100010];
        int iqq = 0;
        for(int i = 1;i <= n;i ++)
        {
            if(inin[i] == 0)
               qqueue[iqq ++] = i;
        }
        for(int i = 0;i < iqq;i ++)
        {
            for(int j = head[qqueue[i]];j != 0;j = Edges[j].next)
            {
                inin[Edges[j].to] --;
                if(inin[Edges[j].to] == 0)
                   qqueue[iqq ++] = Edges[j].to;
            }
        }
        if(iqq != n)
           the_last_ans = 1;
        int queue[100010];
        int iq = 0;
        for(int i = 1;i <= n;i ++)
        {
            if(indegree[i] == 0)
               queue[++ iq] = i;
        }
        int ans = 0;
        sum[++ ans] = iq;
        int head_first = 1;int tail_end = iq;
        int xin_sum = 0;
        while(1)
        {
            if(the_last_ans == 1)
                break ;
            if(tail_end == n)
                break ;
            p = tail_end;
            for(int i = head_first;i <= p;i ++)
            {
               for(int j = head[queue[i]];j != 0;j = Edges[j].next)
                {
                    //printf("%d   %d\n",queue[i],Edges[j].to);
                    indegree[Edges[j].to] --;
                    if(indegree[Edges[j].to] == 0)
                    {
                        queue[++ tail_end] = Edges[j].to;
                        xin_sum ++;
                    }
                }
            }
            sum[++ ans] = xin_sum;
            head_first = p + 1;
            xin_sum = 0;
        }
        if(the_last_ans == 1)
            printf("-1\n");
        else
        {
            int i = 888;
            for(int k = 1;k <= ans;k ++)
            {
                the_last_sum += i * sum[k];
                i ++;
            }
            printf("%d\n",the_last_sum);
        }
    }
}
//

 

posted on 2013-10-16 01:04  天使是一个善良的神  阅读(201)  评论(0编辑  收藏  举报

导航