Reward

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

Total Submission(s): 5636    Accepted Submission(s): 1712

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
题意:

 老板要给非常多员工发奖金。 可是部分员工有个虚伪心态, 觉得自己的奖金必须比某些人高才心理平衡。 可是老板非常人道。 想满足全部人的要求。 而且非常吝啬,想画的钱最少,问满足全部人的前提下最少花多少钱。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 11000
using namespace std; char map[maxn];
int indu[maxn];
int head[maxn], cnt;
int n, m;
int a[maxn]; struct node {
int u, v, next;
}; node edge[110000]; void init(){
cnt = 0;
memset(head, -1, sizeof(head));
memset(indu, 0, sizeof(indu));
for(int i = 1; i <= n; ++i)//全部人的工资一開始都为888
a[i] = 888;
} void add(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
} void input(){
while(m--){
int a, b;
scanf("%d%d", &b, &a);
add(a, b);//注意是反向的, wa了一次
indu[b]++;
}
} void topsort(){
queue<int >q;
int sum = 0;
int ans = 0;
for(int i = 1; i <= n; ++i){
if(!indu[i]){
q.push(i);
ans++;
}
}
while(!q.empty()){
int u = q.front();
sum += a[u];
q.pop();
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
indu[v]--;
if(!indu[v]){
q.push(v);
a[v] = a[u] + 1;//保证后一个人的工资比前一个人的工资高
ans++;
}
}
}
if(ans == n)
printf("%d\n", sum);
else
printf("-1\n");
} int main (){
while(scanf("%d%d", &n, &m) != EOF){
init();
input();
topsort();
}
return 0;
}

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

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

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

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

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

  3. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

  4. hdu 2647 Reward(拓扑排序+优先队列)

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

  5. hdu 2647 Reward(拓扑排序+反图)

    题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...

  6. HDU 2647 逆向拓扑排序

    令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...

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

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

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

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

  9. HDU 2647 Reward【反向拓扑排序】

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

  10. hdu 2647 Reward

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

随机推荐

  1. Codeforces Round #301 (Div. 2) D. Bad Luck Island 概率DP

    D. Bad Luck Island Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/pr ...

  2. Java中应该返回零长度数组或空集合,而不是返回null(转)

    说明:为了避免在数组和集合的获取上增加null的判断,同时也能减少不必要的空指针异常,通常会在业务返回零数组或空集合. 方法: 1.数组: 定义全局静态常量来减少内存开销:private static ...

  3. PWM DAC Low Pass Filtering

    [TI博客大赛][原创]LM3S811之基于PWM的DAC http://bbs.ednchina.com/BLOG_ARTICLE_3005301.HTM http://www.fpga4fun.c ...

  4. java多线程知识点汇总(四)多线程知识点脉络图

    1.多线程安全问题 1)synchronized关键字:如何加锁的问题,选择synchronized方法还是synchnized代码块. 选择哪个锁问题,this对象,还是class对象(针对stat ...

  5. 【资料】wod辅法卷轴

    辅助:1.非口袋2.口袋3.特殊卷附录:腐法装备 第一部分:非口袋 卷轴装备位置效果修正:颈 +2等级修正单手 +2等级修正双手 +4等级修正戒指 无等级修正 后缀颈 黑曜石魔法护身符单手 黑曜石魔法 ...

  6. Eclipse搭建Gradle环境

    转自:http://blog.sina.com.cn/s/blog_4b20ae2e0102uz4t.html 1.上Grandle官网下载Gradle,地址:http://www.gradle.or ...

  7. java集合(ArrayList,Vector,LinkedList,HashSet,TreeSet的功能详解)

    说起集合,我们会潜意识里想到另外一个与之相近的名词——数组,OK!两者确实有相似之处,但也正是这点才是我们应该注意的地方,下面简单列出了两者的区别(具体功能的不同学习这篇文章后就会明白了): 数组 长 ...

  8. iOS: 删除真机测试的Provisioning Profile后,在Code Singing中出现entitlements.plist文件无效,解决办法如下:

    问题主题:method to The entitlements specified in your application’s Code Signing Entitlements file do no ...

  9. M1卡说明及使用proxmark3破解方法

    看了网上写的一些关于M1卡的文章,多数有些误导之嫌.首先谈谈M1卡的规格,M1卡的容量为1KB,好多网上写8KB,这里其实是有个误区,应该是8K位.1Byte=1B=8位.其实也就是说8k位想到于1K ...

  10. 小课堂week19 编程范式巡礼最终季 超级范式

    编程范式巡礼(最终季)--超级范式 本周是编程范式系列的最后一次分享,让我们拉长视角,看向远方,进入"元编程"的领域,在<冒号课堂>中起了个很酷的名字:"超级 ...