HDOJ 2647 Reward(拓扑排序+逆向建图)

Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14214 Accepted Submission(s): 4551

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元,但是有的员工会和其他员工比较,比如1号员工和2号员工比较后,发现两个人的奖金一样,这是1号员工会向老板提出自己的奖金应该比2号员工多,有许多这样的员工对(输入),老板要满足每个人的想法,并且老板所发的奖金最少。最后求老板发了多少奖金。注:如果1号员工和2号员工都向老板提出自己的奖金应该比对方多,这时不存在最少的发放奖金额,输出-1

思路:这题就是要保证前一个员工的奖金比后面的员工的奖金高,联想到拓扑排序中若有路径x->y则必有x在y前面,通过这个性质保证奖金的数量关系,所以尝试拓扑排序。假设有员工对(x, y)输入,表示x的奖金应该比y的奖金多,我首先想到建立x->y的一条有向边,最后要求的是老板一共发了多少奖金,假设图中有路径x->y->z,拓扑排序时,最初想的是直接让x的奖金为:money[x] = money[y] + 1,但是仔细考虑发现当继续拓扑排序到y->z,有money[y] = money[z] + 1,这时改变了y获得的奖金money[y],这时不再有money[x] > money[y],所以我们应该最先修改money[z],最后修改money[x],这时我们可以反向建立图,让输入(x, y)表示y->x,假设有一路径z->y->x,根据拓扑排序我们可以保证money[x] > money[y] > money[z],这样最后把所有员工获得的奖金加起来就是总数。

#include "iostream"
#include "vector"
#include "queue"
using namespace std;

int N, M;

typedef struct{
    int x, y;
} Edge;

int tuopu(vector<Edge> & edges, vector<int> &degree, vector<int>& money){
    queue<int> q;
    int result = 0;
    for (int i = 1; i <= N;i++){
        if(degree[i]==0){
            q.push(i);
        }
    }

    while(!q.empty()){
        int point = q.front();
        q.pop();
        result++;
        for (int i = 0; i < edges.size();i++){
            if(edges[i].y==point){
                if(--degree[edges[i].x] == 0){
                    q.push(edges[i].x);
                    money[edges[i].x] = money[edges[i].y] + 1;
                }
            }
        }
    }
    return result;
}

int main(){
    while(cin >> N >> M){
        vector<int> degree(N + 1, 0);
        vector<int> money(N + 1, 888);
        vector<Edge> edges;
        for (int i = 1; i <= M;i++){//为了输出钱的数量,逆序建立有向图
            Edge edge;
            cin >> edge.x >> edge.y;//逆序。此时边为y --> x
            edges.push_back(edge);
            degree[edge.x]++;//将x作为入度顶点
        }
        int result = tuopu(edges, degree, money);
        if(result == N){
            long sum = 0;
            for (int i = 1; i <= N;i++){
                sum += money[i];
            }
            cout << sum << endl;
        }
        else{
            cout << -1 << endl;
        }
    }
    //system("pause");
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值