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

来源:互联网 发布:linux编辑文本命令 编辑:程序博客网 时间:2024/04/27 08:59

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 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 11 22 21 22 1
 

Sample Output
1777-1
 

Author
dandelion
 

Source
曾是惊鸿照影来
 

Recommend
yifenfei


题目大意:

n个人,m条边,每条边a,b 表示a比b的工资要多,每个人的工资至少888,问满足关系的工资总和至少多少?如果出现关系矛盾,输出-1


解题思路:

根据工资关系建立拓扑图,0入度的人工资从888开始,一层一层,逐渐增加工资,若最后还有人入度不为0,则出现矛盾。


参考代码:

#include <iostream>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int MAXN = 10010;int inDegree[MAXN], ans, cnt, n, m;vector<int> child[MAXN];struct State {    int reward, node;    State(int _reward, int _node) : reward(_reward), node(_node) {}};queue<State> q;void init() {    memset(inDegree, 0, sizeof(inDegree));    for (int i = 0; i <= n; i++) {        child[i].clear();    }    ans = cnt = 0;    while (!q.empty()) q.pop();}void input() {    for (int i = 0; i < m; i++) {        int a, b;        cin >> a >> b;        inDegree[a]++;        child[b].push_back(a);    }}void work() {    for (int i = 1; i <= n; i++) {        if (inDegree[i] == 0) {            q.push(State(888, i));            cnt++;        }    }    while (!q.empty()) {        State cur = q.front();        q.pop();        ans += cur.reward;        for (int i = 0; i < child[cur.node].size(); i++) {            inDegree[child[cur.node][i]]--;            if (inDegree[child[cur.node][i]] == 0) {                q.push(State(cur.reward+1, child[cur.node][i]));                cnt++;            }        }    }}void output() {    if (cnt == n) {        cout << ans << endl;    } else {        cout << -1 << endl;    }}int main() {    ios::sync_with_stdio(false);    while (cin >> n >> m) {        init();        input();        work();        output();    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 不小心踩到仓鼠吐血了怎么办 不小心踩到仓鼠的头怎么办 母仓鼠生完孩子特别暴躁怎么办 被仓鼠咬出血怎么办要不要打针 被小仓鼠咬出血了该怎么办 仓鼠咬破手指流血了该怎么办 小仓鼠的手被棉花丝绑住了怎么办 荷兰猪如果不小心撞流血了怎么办 帮同事刷杯子但把杯子弄坏了怎么办 贵宾犬喜欢张开嘴巴跟人玩怎么办 鼻烟放了几天感觉跟受潮一样怎么办 两个月宝宝鼻屎堵住鼻子怎么办 肉包着智齿怎么办洗燥电影完整版 大腿内侧突然长黑痣越来越大怎么办 肚脐发炎、不脱落怎么办_妈网百科 老公出轨找个按摩的还不离婚怎么办 别人拿错钥匙装修好了房子怎么办 撞车后对方全责却不赔钱怎么办 我的世界国服客户端都是英文怎么办 下载的3d模型没有材质怎么办 写字时习惯把本子放歪该怎么办 被蚊子咬后留下硬疙瘩怎么办 吃冰镇西瓜导致呼吸不顺怎么办 吃冰镇西瓜嚓气导致呼吸不顺怎么办 在pr里面找不到做字幕的怎么办 电脑主机电源坏了接口坏了怎么办 邻居捡到我手机不给这种情况怎么办 婆婆总在儿子面前说媳妇坏话怎么办 试管准备取卵雌激素低该怎么办 订了火车票了忘记带身份证怎么办 网上买火车票忘记带身份证了怎么办 网上买的火车票忘记带身份证怎么办 深圳东站不卖站台票要送客怎么办 网吧刷身份证机器突然不好使怎么办 买了个衣服穿起来有点老气怎么办 美股主动退市股票钱怎么办 新注册的微信号被实名认证了怎么办 他出轨对我冷暴力孩子小怎么办 给情人发红包被老婆发现了怎么办一 情人和我出轨被他老婆发现了怎么办 情人老婆再次发我的存在怎么办