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. 

InputOne 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.OutputFor 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 题意:输入两个数n,m。n表示公司人数,m表示不同人之间的关系。a b表示a的工资大于b.
题解:拓扑排序,找入度为0的点,所有的拓扑完没有剩余,说明没有环
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e4+;
const int mod = 1e9+;
int degree[maxn],add[maxn];
vector<int>G[maxn];
int n,m;
int toposort()
{
queue<int>que;
for(int i=;i<=n;i++)
if(degree[i] == )
que.push(i);
int ans = ;
while(que.size())
{
int ptr = que.front();
que.pop();
ans++;
for(int i=;i<G[ptr].size();i++)
{
degree[G[ptr][i]]--;
if(degree[G[ptr][i]] == )
{
que.push(G[ptr][i]);
add[G[ptr][i]] = add[ptr] + ;
}
}
}
if(ans != n)
return -;
int sum = ;
for(int i=;i<=n;i++)
sum += add[i] + ;
return sum;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(degree,,sizeof degree);
memset(add,,sizeof add);
for(int i=;i<=n;i++)
G[i].clear();
for(int i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[b].push_back(a);
degree[a]++;
}
printf("%d\n",toposort());
}
}

Reward HDU - 2647的更多相关文章

  1. 逆拓扑排序 Reward HDU - 2647

    Reward HDU - 2647 题意:每个人的起始金额是888,有些人觉得自己做的比另一个人好所以应该多得一些钱,问最少需要花多少钱,如果不能满足所有员工的要求,输出 -1 样例1: 2 1 1 ...

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

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

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

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

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

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

  5. hdu 2647 Reward

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

  6. HDU 2647 Reward (拓扑排序)

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

  7. hdu 2647 (拓扑排序 邻接表建图的模板) Reward

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...

  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 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...

随机推荐

  1. select标签使用 三目运算符

    <td> <select id="roleName" name="roleName" class="input" styl ...

  2. CSS改变placeholder的颜色

    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #a1a1a1; } ::-moz-placeholder { /* Mozilla ...

  3. 某地理位置模拟APP从壳流程分析到破解

    工具与环境 Xposed IDA 6.8 JEB 2.2.5 Fiddler2 010Editor NEXUS 5  Android 4.4 好久不玩逆向怕调试器生锈,拿出来磨磨! 高手莫要见笑,仅供 ...

  4. Android基础Activity篇——Intent返回数据给上一个活动

    1.如果活动B要将数据返回给活动A,那么需要以下三步: 1.1在活动A中使用startActivityForResult()方法启动活动B. 1.2在活动B中使用setResult()方法传回Iten ...

  5. HttpWebRequest Post请求webapi

    1.WebApi设置成Post请求在方法名加特性[HttpPost]或者方法名以Post开头如下截图: 2.使用(服务端要与客户端对应起来)[单一字符串方式]:注意:ContentType = &qu ...

  6. XFS: Cross Frame Script (跨框架脚本) 攻击。

    一.Cross Frame Script (跨框架脚本) 攻击什么是Cross Frame Script?很简单,做个实验就知道了.把下面的这段HTML代码另存为一个html文件,然后用ie浏览器打开 ...

  7. TP5.1:连接数据库(全局配置、动态配置、DSN配置)

    前提: (1)在app\index\controller文件下新建一个名为Connect.php的控制器文件 (2)建立一个名为user_curd数据库,里面有一张user表,表内容为: 通过全局配置 ...

  8. Lubuntu , ubuntu 搜索文件

    使用命令行方式搜索 $ locate your_filename

  9. Sliding Window - The Smallest Window II(AIZU) && Leetcode 76

    http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_3_B For a given array a1,a2,a3,...,aNa1 ...

  10. 使用dao时,如何同时使用动态表名和过滤字段?

    使用dao时,如何同时使用动态表名和过滤字段?  发布于 630天前  作者 wukonggg  316 次浏览  复制  上一个帖子  下一个帖子  标签: 无 如题.求样例代码 1 回复 wend ...