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 Beta Round #8 C. Looking for Order 状压

    C. Looking for Order 题目连接: http://www.codeforces.com/contest/8/problem/C Description Girl Lena likes ...

  2. HDU 5641 King's Phone 模拟

    King's Phone 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5641 Description In a military parade, ...

  3. iOS 本地消息推送机制

    转发自:https://www.jianshu.com/p/e347f999ed95     //已经废除的 http://blog.csdn.net/three_zhang/article/deta ...

  4. HTML5开发的翻页效果实例

    简介2010年F-i.com和Google Chrome团队合力致力于主题为<20 Things I Learned about Browsers and the Web>(www.20t ...

  5. JavaScript中的call和apply应用

    ECMAScript3给Function的原型定义了两个方法,他们是Function.prototype.call 和 Function.prototype.apply. 在实际开发中,特别是在一些函 ...

  6. osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld

    环境准备: jdk版本号 jdk:1.7 karaf: 版本号:apache-karaf-3.0.1 下载地址: http://pan.baidu.com/s/1qWM4Y1u http://kara ...

  7. Android 获得view的宽和高

    转自:http://blog.csdn.net/yangdeli888/article/details/25405263 在oncreate()中利用view.getWidth()或是view.get ...

  8. MySQL -- 调优

    多数时候数据库会成为整个系统的瓶颈,比如大的数据量的插入与修改,频繁的亦或是高流量的访问,都会对数据库系统带来很大的压力.我在平时工作的时候,总是会遇到大数据量的插入.修改或是查询的操作,所以在工作的 ...

  9. java Class的 getSuperclass与getGenericSuperclass区别

    Class的getInterfaces与getGenericInterface区别 http://www.cnblogs.com/maokun/p/6773076.html 一.getSupercla ...

  10. hadoop中InputFormat 接口的设计与实现

    InputFormat 主要用于描述输入数据的格式, 它提供以下两个功能.❑数据切分:按照某个策略将输入数据切分成若干个 split, 以便确定 Map Task 个数以及对应的 split.❑为 M ...