Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11836 Accepted Submission(s): 3804 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 Author
dandelion Source
曾是惊鸿照影来

【题意】:
老板打算给员工们发奖励,但是那些员工会去比较各自的奖励,并且会存在一些要求,例如,a员工(后继)的奖励要比b员工的奖励多。然而老板想要知道自己最少需要多少钱俩奖励员工。那么,老板决定每个员工的最低奖励为888元,这是个吉利的数字,现在第一行输入n跟m,n代表员工的数量,m代表有m个要求,接下来的m行就是员工的要求。每行有两个数a,b,表示a的奖励一定要比b的多。最后让你输出**最低的资金**。如果不能达到员工的要求及输出-1。

【分析】:
给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... 让你求最小的值,但是中间出现有向环就不行了,输出-1。
拓扑排序队列实现,因为叶子是最小的,所以把边反向就可以求了。
a的工资比b高,所以输入a b ,建边b——>a
反向建图, 然后top排序分层次; 第一次的工资为888(最低), 第二层的工资 + 1, 后面一样

【代码】:
```
#include

using namespace std;

const int maxn = 1e5 + 10;

const int mod = 142857;

int t,n,m,k,x,u,v,num,ans;

vector G[maxn];

int inDeg[maxn];

int sum[maxn];

queue q;

int topSort()

{

num=0;

while(!q.empty()) q.pop();

for(int i=1;i<=n;i++) if(!inDeg[i]) q.push(i);

while(!q.empty())

{

num++;

int now = q.front();

q.pop();

for(int i=0;i<G[now].size();i++)

{

int nxt = G[now][i];

if(--inDeg[nxt] == 0)

{

q.push(nxt);

sum[nxt]=sum[now]+1;

}

}

}

if(num != n) return -1;

int ans=0;

for(int i=1;i<=n;i++)

ans+=sum[i]+888;

return ans;

}

int main()

{

while(~scanf("%d%d",&n,&m))

{

memset(inDeg,0,sizeof(inDeg));

memset(sum,0,sizeof(sum));

for(int i=1;i<=n;i++) G[i].clear();

while(m--)

{

scanf("%d%d",&u,&v);

G[v].push_back(u); //反向建边

inDeg[u]++;

}

printf("%d\n",topSort());

}

}

HDU 2647 Reward【反向拓扑排序】的更多相关文章

  1. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

  2. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

  3. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  4. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. hdu 2647 Reward(拓扑排序,反着来)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  6. HDU 4857 (反向拓扑排序 + 优先队列)

    题意:有N个人,M个优先级a,b表示a优先于b.而且每一个人有个编号的优先级.输出顺序. 思路来自:与PKU3687一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...

  7. 杭电 2647 Reward (拓扑排序反着排)

    Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to ...

  8. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  9. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  10. 逃生(HDU4857 + 反向拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...

随机推荐

  1. WebSocket添加事件监听器(6)

    WebSocket编程遵循异步编程模型;打开socket后,只需要等待事件发生,而不需要主动向服务器轮询,所以需要在WebSocket对象中添加回调函数来监听事件. WebSocket对象有三个事件: ...

  2. 【题解】JSOI2009游戏

    真的没想到...果然反应太迟钝,看到题目毫无思路,一点联想都没有. 按照网上博客的说法:一眼棋盘染色二分->二分图->最大匹配->BINGO?果然我还是太弱了…… 我们将棋盘黑白染色 ...

  3. 周记【距gdoi:105天】

    月考果然很可怕,跪得要死. 然后这周搞(被老师坑)去搞某个程序,我和蔡大神和kpm分工搞(结果最后我也只是变成全程嘴炮). 这周有点闷,明明想快乐点但还是…… 进度慢得要死,后缀数组略神的东西.模仿了 ...

  4. BZOJ3211 花神游历各国 【树状数组 + 并查集】

    题目 输入格式 输出格式 每次x=1时,每行一个整数,表示这次旅行的开心度 输入样例 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4 输出样例 101 11 1 ...

  5. MySQL:BlackHole

    MySQL:BlackHole 顾名思义BlackHole就是黑洞,只有写入没有输出.现在就来实验一下吧 首先查看一下MySQL支持的存储引擎 mysql> show engines;+---- ...

  6. Any gotchas at all with converting from MyISAM to InnoDB?

    Q: I'm ready to move from MyISAM to InnoDB but wanted to know if there was a full list of things to ...

  7. net.sf.json与fastjson两种jar包的使用

    首先说清楚:这两种方式是进行json解析的两种不同的方式而已,哪一种都可以. 一.引入net.sf.json包 首先用net.sf.json包,当然你要导入很多包来支持commons-beanutil ...

  8. bzoj 3720 Gty的妹子树 树分块?瞎搞

    Gty的妹子树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2149  Solved: 781[Submit][Status][Discuss] D ...

  9. 慕课网javascript 进阶篇 第九章 编程练习

    把平常撸的码来博客上再撸一遍既可以加深理解,又可以理清思维.还是很纯很纯的小白,各位看官老爷们,不要嫌弃.最近都是晚睡,昨晚也不例外,两点多睡的.故,八点起来的人不是很舒服,脑袋有点晕呼呼,鉴于昨晚看 ...

  10. 【hdu1712】分组背包(每组最多选1个)

    [分组背包] [题意]ACboy要开始选课了,上一门课能够获得的收益和他上这门课的时间是有关的,然后给你若干门课,让你帮他进行选课,每一门课自然是只能选择一个课程时长,问你如何选择,才能使ACboy获 ...