hdu 2647 Reward

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
/*/
题意:
老板发工资,但是老板很小气,但是至少每个人得发888元,而且,每个员工之间有业绩比较,业绩高的工资得高,至少多1元,问至少要发多少钱。 思路:
拓扑排序,查找有多少个不同等级的点,每个点的值与前面累加1; 计总和加上每个人888. 简单的拓扑排序,没必要多说了: AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"string"
#include"cstdio"
#include"vector"
#include"cmath"
#include"queue"
using namespace std;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define MX 10005 int indegree[MX];
vector<int>next_v[MX]; void init() {
for(int i=0; i<MX; i++)
next_v[i].clear();
memset(indegree,0);
} int toposort(int n) {
int p[MX];
int cnt=0,money=0,num,summoney=n*888 ;
while(cnt!=n) {
num=0;
for(int i=1; i<=n; i++) {
if(!indegree[i]) {
indegree[i]=-1;
p[num]=i;
num++;
}
}
if(!num) {//没有员工,工资最高,不能排序,返回错误;
return -1;
} else {
summoney+=money*num;
money++;
cnt+=num; //表示已经历遍了这么多个员工
for(int i=0; i<num; i++) {
for(int j=0; j<next_v[p[i]].size(); j++) {
indegree[next_v[p[i]][j]]--;
}
}
}
}
return summoney;
} int main() {
int n,m,a,b;
while(~scanf("%d%d",&n,&m)) {
init();
for(int qq=0; qq<m; qq++) {
scanf("%d%d",&a,&b);
int flag=0;
for(int i=0; i<next_v[b].size(); i++) {
if(next_v[b][i]==a)
flag=1;
}
if(!flag) {
indegree[a]++;
next_v[b].push_back(a);
}
}
printf("%d\n",toposort(n));
}
return 0;
}
  

ACM: hdu 2647 Reward -拓扑排序的更多相关文章

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

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

  2. HDU 2647 Reward (拓扑排序)

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

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

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

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

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

  5. HDU 2647 逆向拓扑排序

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

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

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

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

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

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

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

  9. hdu 2647 Reward

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

随机推荐

  1. Jquery 自定义弹窗等待

    (function ($) { $.extend({ //弹窗蒙层 ShowLoadDialog : function () { ) { var cusrtxt = $("<div i ...

  2. [LeetCode] Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  3. zoj 3888 线段树 ***

    卡n^2,用线段树降到nlogn 记录每个点上所覆盖线段的次小值,保证能有两条路径能走 #include<cstdio> #include<iostream> #include ...

  4. IOS登陆+注册+抽奖+排行榜

    要求:三个页面(登录页面,pickerView页面,排行榜页面),pickerView页面是三个组件,每个组件显示0-9,点击按钮进行随机,获得的值存入排行榜,排行榜显示大于500的最高的10个分数和 ...

  5. 用C获得当前系统时间(转)

    #include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo ...

  6. MongoDB学习(1)—在Windows系统中安装MongoDB

    概述 本文主要介绍在Windows系统安装MongoDB的方法. MongoDB官方网址:http://www.mongodb.org/,最新版本为2.6.7. 注意: 从2.2版本开始,MongoD ...

  7. 智能车学习(二十三)——浅谈心得体会

          因为毕竟是竞赛,跟学校挂钩,没办法开源代码和算法完成思路,所以不能详细写太多,如果可以等价交换的话,应该还是可以向领导申请一下的.       在厦大信科通信系,参加这个比赛,大家都觉得性 ...

  8. Linux学习笔记(2)Linux学习注意事项

    1 学习Linux的注意事项 ① Linux严格区分大小写 ② Linux中所有内容均以文件形式保存,包括硬件,如硬件文件是/deb/sd[a-p] ③ Linux不靠扩展名区分文件类型,但有的文件是 ...

  9. RPC和Socket,RMI和RPC之间的关系

    远程通信机制RPC与RMI的关系 http://blog.csdn.net/zolalad/article/details/25161133       1.RPC RPC(Remote Proced ...

  10. JavaWeb监听器详解

    1 JavaWeb监听器概述 在JavaWeb被监听的事件源为:ServletContext.HttpSession.ServletRequest,即三大域对象.有监听域对象"创建" ...