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. JQ 使用模板

    1.首先需要添加模板JS文件,

  2. Eclipse打开.class文件

    1.准备材料 jad.exe    下载地址  https://varaneckas.com/jad/,根据系统去选择(就当是废话)  下载下来的是jad158g.win.zip压缩文件,解压后有个j ...

  3. gitignore : VisualStudio.gitignore

    https://github.com/github/gitignore/blob/master/VisualStudio.gitignore ## Ignore Visual Studio tempo ...

  4. 【转】2012年6月26 – PPS网络电视PHP工程师最新面试题

    每一次面试都是一场较量,和面试官,更是和你自己! 前言:虽然面试职位是PHP工程师,但题目仅绝非限于PHP,甚至都没有多少PHP的题!inner peace!希望能给你带了一丝帮助. PPS网络电视面 ...

  5. UVa 637 - Booklet Printing

    题目:模拟输出n页书的装订打印状态. 分析:模拟.页数为(n+3)/ 4,仅仅有n不超过半篇时会输出半篇. 说明:好多曾经做过的题目(⊙_⊙). #include <cstdlib> #i ...

  6. 【资料】wod书籍

    世界掉落 特点 风化的书卷 可用三次的无限耗材 华丽的书卷 可用5次 无限耗材 队伍唯一 抄录页:新手躲避 近远防御 +34%X技能等级 风化的书卷:新手躲避 华丽的书卷:新手躲避 抄录页:高级闪避技 ...

  7. Redis 起步(linux)

    Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介 Redis是 一个key-value存储系统.和Memcached类似,但是解决 ...

  8. Single Number and Single Number II

    [] Given an array of integers, every element appears twice except for one. Find that single one. [] ...

  9. hello-循环神经网络(RNN)原理

    主要的应用:机器翻译,自然语言处理,文本处理,语音识别, 图像描述生成 (Generating Image Descriptions), 图像问答QA.... 循环神经网络(RNN)原理通俗解释 1. ...

  10. 监听内容变化 TextWatcher @功能

    监听多个TextView的内容变化 使用示例 TextWatcherUtils.addTextChangedListener(isAllNotEmpty -> btnLogin.setEnabl ...