HDU2647 Reward 【拓扑排序】

Reward

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

Total Submission(s): 3927    Accepted Submission(s): 1199

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

题意:有n个员工,有些员工要求自己的奖金要比另外的一些高,求至少发放多少奖金能满足所有人的要求,若没法满足就输出-1;

题解:可以把每个人的需求想象成层数,若没有需求就在第一层,奖金一层层的发,每增加一层就加一块钱,这样就能满足最终发的奖金数额最小。需要逆序拓扑,另外这题碰到了个奇怪的现象,开始我的map数组开小了,但是提交的时候给的结果是TLE而不是RE,把数组加了一倍后就AC了,看来OJ判题系统并不是太精确。

#include <stdio.h>
#include <string.h>
#define maxn 10002

int ans, queue[maxn];
struct Node{
    int to, next, val;
} map[maxn << 1];
struct node{
    int first, money, indegree;
} head[maxn];

bool topoSort(int n)
{
    int i, front = 0, back = 0, u;
    for(i = 1; i <= n; ++i)
        if(!head[i].indegree) queue[back++] = i;
    while(front != back){
        u = queue[front++];
        ans += head[u].money;
        for(i = head[u].first; i != 0; i = map[i].next)
            if(!--head[map[i].to].indegree){
                head[map[i].to].money = head[u].money + 1;
                queue[back++] = map[i].to;
            }
    }
    return back == n;
}

int main()
{
    int n, m, a, b, i;
    while(scanf("%d%d", &n, &m) != EOF){
        memset(head, 0, sizeof(head));
        for(i = 1; i <= m; ++i){
            scanf("%d%d", &a, &b);
            map[i].to = a;
            map[i].next = head[b].first;
            ++head[a].indegree;
            head[b].first = i;
        }
        ans = 888 * n;
        if(!topoSort(n)) printf("-1\n");
        else printf("%d\n", ans);
    }
    return 0;
}

HDU2647 Reward 【拓扑排序】

时间: 2024-10-12 22:03:14

HDU2647 Reward 【拓扑排序】的相关文章

hdu2647 Reward 拓扑排序

此题的关键在于分层次,最低一层的人的奖金是888,第二层是888+1 …… 分层可以这样实现.建立反向图.在拓扑排序的时候,第一批入度为0的点就处于第一层,第二批处于第二层 …… 由于是逐个遍历入度为0的点,所以怎么实现上面所说的第一批,第二批就需要动点脑. 可以试试下面的测试数据: 4 31 32 34 3 4 31 22 34 3 4 21 23 4 6 31 23 45 6 测试结果依次是: 3555 3556 3554  5331 #include<iostream> #include

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 wa

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

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

hdu2647(拓扑排序)

这道题题意很简单,老板给员工发福利,有些员工要求自己的福利必须比某个人高,老板希望在满足所有人的要求下,总花费最小. 拓扑排序分层,反向建表,正向也可以,只不过计算稍微麻烦些,但更接近题意. 这道题我还是卡了一会的,一开始用下标模拟堆栈的方法wa了好多次,后来试着调用stl的栈,接着wa,才发现是自己的分层策略和栈的性质不相容. 我的分层策略是,若有个点,在删去一个边之后入度变为0,则这个点的层数为刚刚删掉的那条边的起点的层数加1. 使用这种策略加上用栈会在处理一些特殊情况是发生错误.后来改为用

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> #

HDU2647(拓扑排序+反向建图)

题意不说了,说下思路. 给出的关系是a要求的工资要比b的工资多,由于尽可能的让老板少付钱,那么a的工资就是b的工资+1,可以确定关系为a>b,根据拓扑排序建边的原则是把"小于"关系看成有向边,那么我们可以建边v->u. #include <stdio.h> #include <string.h> #include <string> #include <iostream> #include <algorithm> #