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.
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.
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
- HDU 2647 Reward(图论-拓扑排序)
- HDU 2647 Reward(图论-拓扑排序)
- HDU 2647 Reward(拓扑排序)
- hdu 2647 Reward (拓扑排序)
- hdu 2647 Reward ( 拓扑排序 )
- HDU 2647 - Reward(拓扑排序)
- hdu 2647 Reward (拓扑排序)
- HDU 2647Reward(拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 -- Reward (拓扑排序)
- hdu 2647 Reward(拓扑排序)
- HDU-2647 Reward(拓扑排序)
- HDU 2647Reward (拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward (拓扑排序)
- HDU 2647 Reward(拓扑排序)
- android 裁剪涂抹区域并保存成图片
- 【翻译自mos文章】参数Db_recovery_file_dest_size的最大值是多少
- ASP.NET页面请求原理浅析
- HDOJ-1087-Super Jumping! Jumping! Jumping! 解题报告
- 求商求模运算
- HDU 2647 Reward(图论-拓扑排序)
- eclipse 技巧
- 本人很迷茫的说
- 给定一个二进制数,要求循环移位,在原二进制数中操作(C语言)
- TF-IDF
- 雾山的Java学习笔记---I/O(三)(处理流)
- Ubuntu 下字体问题链接搜集
- Visual C++ 2012入门经典(第6版) 课后练习(第06章)
- static的含义以及其在C/C++中的区别