Reward

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 51   Accepted Submission(s) : 21

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int i,n,m;
int a[],cnt[];
vector<int> s[];
long long sum;
bool toposort()
{
queue<int> Q;
int num=;
for(int i=;i<=n;i++)
if (cnt[i]==) {Q.push(i); a[i]=;}
while(!Q.empty())
{
int u=Q.front();
num++;
Q.pop();
for(int i=;i<s[u].size();i++)
{
cnt[s[u][i]]--;
if (cnt[s[u][i]]==)
{
Q.push(s[u][i]);
a[s[u][i]]=max(a[u]+,a[s[u][i]]);
}
}
}
if (num<n) return ;
return ;
}
int main()
{ while(~scanf("%d%d",&n,&m))
{
for(i=;i<=n;i++) s[i].clear();
memset(cnt,,sizeof(cnt));
for(i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&y,&x);
s[x].push_back(y);
cnt[y]++;
}
memset(a,,sizeof(a));
if (!toposort())
{
printf("-1\n");
continue;
}
sum=;
for(i=;i<=n;i++) sum+=a[i];
printf("%lld\n",sum);
}
return ;
}

hdu 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. 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. Matlab - 矩阵元素引用

    >> A = [ ; ; ] A = 1. 选择第m行n列的元素 >> A(,) ans = 2. 选择第i列所有元素 >> A(:,) ans = 3. 选择第j ...

  2. Block使用中的一些要注意的地方

    本文主要是阐述一下Block中如何的使用外部变量以及block本身的内存管理. 先定义一个block变量,作为后续的例子中使用: typedef void(^BlockCC)(void); Block ...

  3. Cash Machine

    Problem Description A Bank plans to install a machine for cash withdrawal. The machine is able to de ...

  4. quicksort快排

    废话不多说,上代码: void quicksort(int x[], int lo, int hi){ int i = lo, j = hi; ]; while(i <= j){ while(x ...

  5. 一个神奇SQL引发的故障【转】

    前几天一个客户数据库主实例告警,诊断过程中发现是由一个慢SQL导致的数据库故障,而在排查逐步深入之后却发现这个现象的不可思议. 问题描述 2016年12日09日,大概9点26分左右,一个客户的生产库主 ...

  6. sql语句的使用;

    1.导出数据库的语句: mysqldump -u root -p shop > d:\shop.sql

  7. json 与entity/list/map的转换

    一  json -> entity User.java package com.xxx.hotel.train.json.json2entity; import java.io.Serializ ...

  8. C语言指针强制转化的应用

    指针类型强制转化在kernel设计中非常常见,这里记录两个非常有意思的用法: 1.对地址进行运算.任何虚拟地址都表示成void *va  = (void *) 100, *(va + 1) ==101 ...

  9. sql or 与and同时有时要注意

    如果是一天sql语句中有or和and同时在 正确:where xxx=xx and (xxx=xx or xxx=xx) 错误:where xxx=xx and xxx=xx or xxx=xx (这 ...

  10. 好玩的获取目录信息的例子[C#]

    DirectoryInfo dirinfo = new DirectoryInfo("d:\\111"); DirectoryInfo[] dirs = dirinfo.GetDi ...