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.先输入两个整数n,m,代表工人的数量编号1~n和m种规则,接下来的m行每行有两个整数a,b表示a的工资比b高,输入最小钱数,若不存在输出-1。 因为无法确定工资最高的人工资是多少,所以反着排序,让工资最小的在前面,输入a,b时记录b约束a,前驱为0的人工资为888,依次累加。
 #include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int n,m,i,j,num[],head[],mon[],sum,ans;
struct stu
{
int to,next;
}st[];
void init()
{
memset(num,,sizeof(num));
memset(head,-,sizeof(head));
memset(mon,,sizeof(mon));
sum=;
ans=;
}
void add(int a,int b)
{
st[i].to=b;
st[i].next=head[a];
head[a]=i;
num[b]++;
}
void topo()
{
int i,j;
queue<int>que;
while(!que.empty())
{
que.pop();
}
for(i = ; i <= n ; i++)
{
if(num[i] == )
{
mon[i]=;
que.push(i);
}
}
while(!que.empty())
{
ans++;
m=que.front();
que.pop();
sum+=mon[m];
for(i = head[m] ; i != - ; i = st[i].next)
{
if(--num[st[i].to] == )
{
que.push(st[i].to);
mon[st[i].to]=mon[m]+;
}
}
}
if(ans == n)
printf("%d\n",sum);
else
printf("-1\n");
}
int main()
{
int a,b;
while(scanf("%d %d",&n,&m)!=EOF)
{
init();
for(i = ; i < m ; i++)
{
scanf("%d %d",&a,&b);
add(b,a);
}
topo();
} }

 

杭电 2647 Reward (拓扑排序反着排)的更多相关文章

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

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

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

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

  3. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  4. HDU 2647 Reward (拓扑排序)

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

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

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

  6. Reward 杭电 2647

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

  7. HDU 2647 逆向拓扑排序

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

  8. hdu2647 Reward 拓扑排序

    此题的关键在于分层次,最低一层的人的奖金是888,第二层是888+1 …… 分层可以这样实现.建立反向图.在拓扑排序的时候,第一批入度为0的点就处于第一层,第二批处于第二层 …… 由于是逐个遍历入度为 ...

  9. 杭电2000——ASCII码排序

    /* ASCII码排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. c++ 优先级大全

    重置优先级

  2. display:table / display:table-cell 用法

    display:table  元素会作为块级表格来显示(类似table):表格前后带有换行符: display:table-cell 元素会作为表格单元格来显示(类似<td> 和 < ...

  3. discuz x2.5用户注册后邮箱认证后无法收到邮件或者直接进垃圾箱

    又是一个周末,jquery特效继续折腾我那discuz论坛,我开启了个邮箱验证,恶意注册的太恶心了,没有办法. 能稍微屏蔽点,但是问题来了,据亲们反应,无法收到验证邮件,或者有时间直接进入垃圾箱,这个 ...

  4. 使用json传输数组实例

    client.php <?php //遍历数组元素,并将元素转码 function array_iconv($data, $in_charset='GBK', $out_charset='UTF ...

  5. SpringBoot项目不占用端口启动

    @EnableScheduling @SpringBootApplication public class Application { public static void main(String[] ...

  6. iOS面试题之内存管理

    本文围绕内存管理的几种方法展开叙述. 1.内存管理是什么? 内存管理,就是对内存资源进行优化. 2.内存管理的三种方法? Objective-C的内存管理主要有三种方式ARC(自动内存计数).MRC( ...

  7. AJPFX对选择和冒泡两种排序的理解

    冒泡排序和直接选择排序都是排序中比较简单和容易实现的算法,先简单说说两者的区别:先以按照元素从小到大为:冒泡排序:将相邻元素两两比较,如果有比较大的,就把比较大的放在右边,这样的结果就是一轮排序完毕后 ...

  8. 如何配置TomCat

    1.先查看你自己java的jdk的版本号 2.通过jdk版本号确定下载的Tomcat版本 ,因为我的是jdk 1.8的,所以要下载Tomcat 8版本 附上下载官网http://tomcat.apac ...

  9. 接口自动化- 基于 Python

    准备工作 这部分其实在谷歌或者百度上搜索下就可以完成的,可是我就是想再啰嗦一遍,说不定有比我更懒的同学呢哈哈~ 第一步 Python的安装配置 打开官网: https://www.python.org ...

  10. 关于html页面元素语义化的一点思考

    这几天在看招聘公告前端工程师的要求基本都附带了html语义化的要求,所以稍微关注了下这方面的知识.对于其中的一点就是要求页面元素在去除css样式之后还能有良好的布局引发了我一点思考.作为前端刚入门的我 ...