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. bzoj 3969: [WF2013]Low Power 二分

    3969: [WF2013]Low Power Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  2. mmm-master漂移问题的分析

    date:20140527auth:Jin 一.问题描述线上store应用,偶尔出现慢的现象.检查发现是writer角色在master-backup之前漂移检查mysql-log没有发现异常,也没前端 ...

  3. Dual transistor improves current-sense circuit

    In multiple-output power supplies in which a single supply powers circuitry of vastly different curr ...

  4. Activex 数字签名

    本次使用makecert的命令如下: makecert -sv online.pvk -n "CN=中国在线" -ss My -r -b 01/01/1900 -e 01/01/9 ...

  5. 【资料】wod属性

    各个属性的影响力量 st 影响近战远程伤害和体力体质 co 影响体力(比力量的影响大)智力 in 影响法力和魔法防御灵巧 dx 影响近战远程命中和近战躲闪魅力 ch 影响诅咒和治愈能力,诅咒攻击命中和 ...

  6. android 各种颜色值 colors.xml

    <?xml version="1.0" encoding="utf-8" ?> <resources> <color name=& ...

  7. 一个table插件,用于bootstrap开发

    最近项目中改用bootstrap,可以给的通用table,写的有点死,id名称是固定的,那一个页面两个table如何做呢? ok,模仿着别人的代码,写了一个,整体代码如下: ; (function(f ...

  8. iOS网络编程解析协议二:XML数据传输解析

    XML两种解析方式,一种是SAX,NSXMLParser是SAX方法解析,另一种是DOM(Document Object Model); 区别: SAX: 只能读,不能修改,只能顺序访问,适合解析大型 ...

  9. 数学图形(2.14)Spherical helix曲线

    从http://mathworld.wolfram.com/SphericalHelix.html上找到如下一些关于该曲线的说明,不过似乎他的公式和我的脚本完全是两个东西.. The tangent  ...

  10. 使用Proxmark3进行MIFARE Classic卡的安全测试

    使用Proxmark3进行MIFARE Classic卡的安全测试   Proxmark3的MIFARE安全测试是很多朋友都非常重视的一部分,所以我们特地以这个部分进行介绍,告诉大家如何当你完成前期操 ...