您的位置:首页 > 理论基础 > 数据结构算法

hdu 2647 拓扑排序变形

2016-05-11 20:18 344 查看
Reward

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)

Total Submission(s): 6976    Accepted Submission(s): 2163



Problem Description

Dandelion's uncle is a boss of afactory. As the spring festival is coming , he wants to distribute rewards tohis 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 thedistributing of rewards ,just like a's reward should more than b's.Dandelion'sunclue wants to fulfill all the demands, of course ,he wants to use the leastmoney.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 rewardshould be more than b's.

 

 

Output

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

 

 

Sample Input

21

12

22

12

21

 

 

Sample Output

1777

-1

 

 

Author

dandelion

 

 

Source

曾是惊鸿照影来

 

 

 题目大意:给你n个人,m个关系,表示a比b的奖金要多。问最少分配的奖金总数是多少。

题目要求的范围是10000 所以要用邻接链表来做 否则会超内存

因为要a的奖金要比b的多 所以 把a看成入度   把b看成出度

还有一个地方需要注意:判断输出-1的情况不能只判断没有一个入度为0的点,因为有可能在中间就出现矛盾了,如:a——>b——>c——>d——>c有入度为0的点,但却要输出-1;

不懂得地方看下面代码的注释

AC代码:

#include<cstdio>

#include <cstring>

#include<queue>

#include<iostream>

#define MAX20005

using namespacestd;

int n,m,a,b;

inthead[MAX],degree[MAX],money[MAX];

struct node

{

    int to;

    int next;

}e[MAX];

int main()

{

   while(~scanf("%d%d",&n,&m))

    {

        queue<int>s;

        memset(head,-1,sizeof(head));

        memset(degree,0,sizeof(degree));

        for(int i = 1; i <= n; i++)  ///总共n个人  每个人最少888元

            money[i] = 888;

        for(int i = 1; i <= m; i++)

        {

           scanf("%d%d",&a,&b);

            degree[a]++;  ///入度

            e[i].to = a;     

            e[i].next = head[b];    ///e[i].next表示a指向多少个点

            head[b] = i;    ///head数组用来储存第几个点

        }

        for(int i = 1; i <= n; i++)  ///总共n个人

        {

            if(degree[i]==0)

            {

                s.push(i);  ///把入度为零的进队

            }

        }

        int sum = 0;

        int ans = 0;

        while(!s.empty())

        {

            int u = s.front();

            s.pop();

            sum+=money[u];

            ans++;

            for(int j = head[u]; j!=-1; j =e[j].next)  ///通过head数组来找到输入u点的时候是第几次

            {

                if(--degree[e[j].to]==0)   ///把下一个点入队

                {

                    s.push(e[j].to);

                    money[e[j].to] = money[u] +1;   ///下一个点的奖金肯定比当前这个多

                }

            }

        }

        if(ans!=n)   ///如果调用的次数小于n  那么可能中间有矛盾 不能满足条件

            printf("-1\n");

        else

            printf("%d\n",sum);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构