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. Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建

    1.开发工具使用: MyEclipse或Eclipse,数据库使用Oracle.需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理). 2.初始环境配置: ...

  2. 重拾smslib

    http://www.tuicool.com/articles/mm2yQrN http://blog.csdn.net/ll136078/article/details/8737348 http:/ ...

  3. C#的lock关键字

    using System; using System.Threading; namespace Test { class Program { //一.Lock定义 //lock 关键字可以用来确保代码 ...

  4. wifi display代码 分析

    转自:http://blog.csdn.net/lilian0118/article/details/23168531 这一章中我们来看Wifi Display连接过程的建立,包含P2P的部分和RTS ...

  5. 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)

    1.内容观察者ContentObserver 如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,可以在ContentProvider 发生数据变化时调 ...

  6. Delphi的属性Property

    参考:http://www.cnblogs.com/edisonfeng/archive/2012/05/22/2513727.html 一.基本属性 TOnUserInfoShow = proced ...

  7. 如何减少JS的全局变量污染

    A,唯一变量 B,闭包

  8. OCJP(1Z0-851) 模拟题分析(六)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  9. HDU4495 Rectangle

    求组成的等腰三角形面积最大值. 对此题的总结:暴力出奇迹 组成的三角形放置方式一共只有4种,用ans表示目前已知的最长三角形的边长,从上到下,从左到右枚举顶点,再枚举边长,一个重要剪枝是枚举边长l时先 ...

  10. git 本地仓库和远程仓库及本地分支和远程分支

    从远程git仓库签出代码:   $ git clone git://aaa.com/git_project.git (远程git服务器项目所在地址) 当你需要克隆远程项目到本地时,默认会把项目保存在名 ...