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. 不按装mysql情况下,php安装pdo_mysql

    安装pdo时遇到 --with-pdo-mysql  这个要指向mysql安装目录:可是我这台机器不安装mysql; 解决方法: [root@localhost app]#  yum install ...

  2. Codeforces Round #300 A. Cutting Banner 水题

    A. Cutting Banner Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pro ...

  3. ROS知识(3)----功能包package编译的两种方式

    ROS的包编译有两种方法(我知道的),一种是用rosmake,这种方法简单:另一种是用catkin_make,这种方法更方便包的管理和开发.这两种方法都是先建立工作空间workspace(类似于vs下 ...

  4. spring aop 理解

    aop简介 aop是spring 的两大特性之一,还有IOC.主要提供面向切面的编程思想,区分于面向对象编程. aop原理(动态代理+反射) 在一个方法体中,可能会存在很多其他的方法调用,我们可以把每 ...

  5. TCP长连接与短链接

    1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次 ...

  6. Delphi 的插件框架 WisdomPluginFramework

    WisdomPluginFramework是融合OSGI微内核理念 + Eclipse的扩展点概念而精心设计的轻量级插件框架, 由Delphi实现,但可以使用于Delphi.BCB.VC++中,提供非 ...

  7. sourceinsight使用技巧

    转:http://blog.csdn.net/flyyanqu/article/details/2222799 目录(?)[-] 配置成简单好用的cjava代码编辑器 缩进与tab 向项目中添加文件时 ...

  8. Idea下Python开发平台的搭建

    1. python的下载 https://www.python.org/downloads/ 2. idea下python插件的安装 点击File->Settings...->Plugin ...

  9. Unity 面试题

    一:什么是协同程序? 在主线程运行的同时开启另一段逻辑处理,来协助当前程序的执行,协程很像多线程,但是不是多线程,Unity的协程实在每帧结束之后去检测yield的条件是否满足. 二:Unity3d中 ...

  10. 怎样加入cocostudio生成的UI到项目

    cocos2dx版本号:cocos2d-x-3.2alpha0 cocostudio版本号:V1.4.0.1 1.将cocostudio生成的UI文件复制到项目Resources目录 2.加入头文件, ...