hdu 2647 Reward (拓扑排序分层)

Reward

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

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

题意:就是老板要给员工发福利,每个人的贡献不一样,,老板就要根据贡献来进行发福利了,但是老板又想最少的发福利,每个人基本福利是888,若A比B的贡献大,那我们就给A发889。

解题思路:如果用邻接矩阵来做的话,我觉得肯定是要超内存的,所以我选择了用邻接表来处理这一道题。因为是要比较谁比谁大,进行统计,所以我们就反方向的来建图,这样就可以统计出来在每一个层次的人。这样做还是WA了一次,因为一个人可能比多个人贡献要大,所以我们要选取他比那多个人中最大的那个人要多就够了,嘿嘿。

贴出代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 10005

struct ArcNode
{
    int to;
    struct ArcNode * next;
};

struct ArcNode * List[MAXN];
int counting[MAXN], Indegree[MAXN];
int mark;

void Topological(int n)
{
    int top = -1;
    struct ArcNode * temp;
    for(int i = 1; i<=n; i++)    //将度为0的点入栈
    {
        if(0 == Indegree[i])
        {
            Indegree[i] = top;
            top = i;
        }
    }

    for(int i = 1; i<=n; i++)
    {
        if(top == -1)     //判断是否构成环
        {
            mark = 1;
            break;
        }
        else
        {
            int j = top;
            top = Indegree[top];
            temp = List[j];
            while(NULL != temp)
            {
                int k = temp->to;
                counting[k] = counting[k] > counting[j]+1 ? counting[k] : counting[j]+1;   //取最大的贡献
                if(--Indegree[k]  == 0)
                {
                    Indegree[k] = top;
                    top = k;
                }
                temp = temp->next;
            }
        }
    }
}

int main()
{
    int n, m, u, v, num;
    struct ArcNode * temp;
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        memset(counting, 0, sizeof(counting));
        memset(Indegree, 0, sizeof(Indegree));
        memset(List, 0, sizeof(List));
        mark = 0;    num = 0;
        while(m--)
        {
            scanf("%d%d", &u, &v);   //u表示起点,v表示终点
            Indegree[u]++;    //反向建图,记录每个点的入度
            temp = (struct ArcNode *)malloc(sizeof(ArcNode));
            temp->to = u;
            temp->next = NULL;

            if(List[v] == NULL)
                List[v] = temp;
            else
            {
                temp->next = List[v];
                List[v] = temp;
            }
        }

        Topological(n);

        if(mark)
            printf("-1\n");
        else
        {
            for(int i = 1; i<=n; i++)  //将各个点的值累加
                num += counting[i];
            num = num+888*n;
            printf("%d\n", num);
        }
    }

    return 0;
}

hdu 2647 Reward (拓扑排序分层),布布扣,bubuko.com

时间: 2024-11-11 06:44:52

hdu 2647 Reward (拓扑排序分层)的相关文章

ACM: hdu 2647 Reward -拓扑排序

hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

HDU 2647 Reward(拓扑排序)

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

hdu 2647 Reward 拓扑排序。

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4599    Accepted Submission(s): 1400 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

HDU 2647 Reward 拓扑排序

Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

hdu 2647 Reward(拓扑排序+反图)

题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平衡: 但是老板很人道, 想满足所有人的要求, 并且很吝啬,想画的钱最少 输入若干个关系 a b a c c b 意味着a 的工资必须比b的工资高 同时a 的工资比c高: c的工资比b高 当出现环的时候输出-1 #include<iostream> #include<cstring> #

HDU 2647 逆向拓扑排序

令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么in[a]++,再进行拓扑排序,每次都让边的终点的level值大于起始点,那么要用max取较大值 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5

HDU 2647 Reward(图论-拓扑排序)

Reward 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 ,a

HDU 4917 Permutation 拓扑排序的计数

题意: 一个有n个数的排列,给你一些位置上数字的大小关系.求合法的排列有多少种. 思路: 数字的大小关系可以看做是一条有向边,这样以每个位置当点,就可以把整个排列当做一张有向图.而且题目保证有解,所以只一张有向无环图.这样子,我们就可以把排列计数的问题转化为一个图的拓扑排序计数问题. 拓扑排序的做法可以参见ZJU1346 . 因为题目中点的数量比较多,所以无法直接用状压DP. 但是题目中的边数较少,所以不是联通的,而一个连通块的点不超过21个,而且不同连通块之间可以看做相互独立的.所以我们可以对

hdu 4857 逃生 拓扑排序+优先队列,逆向处理

hdu4857 逃生 题目是求拓扑排序,但不是按照字典序最小输出,而是要使较小的数排在最前面. 一开始的错误思路:给每个点确定一个优先级(该点所能到达的最小的点),然后用拓扑排序+优先对列正向处理,正向输出.这是错误的,如下样例: 1 5 4 5 2 4 3 2 1 3 1 正确的解法:是反向建边,点大的优先级高,用拓扑排序+优先队列,逆向输出序列即可. 根据每对限制,可确定拓扑序列,但此时的拓扑序列可能有多个(没有之间关系的点的顺序不定).本题要求较小的点排到前面,则可确定序列. (1)如果点