Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11836 Accepted Submission(s): 3804 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 Author
dandelion Source
曾是惊鸿照影来

【题意】:
老板打算给员工们发奖励,但是那些员工会去比较各自的奖励,并且会存在一些要求,例如,a员工(后继)的奖励要比b员工的奖励多。然而老板想要知道自己最少需要多少钱俩奖励员工。那么,老板决定每个员工的最低奖励为888元,这是个吉利的数字,现在第一行输入n跟m,n代表员工的数量,m代表有m个要求,接下来的m行就是员工的要求。每行有两个数a,b,表示a的奖励一定要比b的多。最后让你输出**最低的资金**。如果不能达到员工的要求及输出-1。

【分析】:
给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... 让你求最小的值,但是中间出现有向环就不行了,输出-1。
拓扑排序队列实现,因为叶子是最小的,所以把边反向就可以求了。
a的工资比b高,所以输入a b ,建边b——>a
反向建图, 然后top排序分层次; 第一次的工资为888(最低), 第二层的工资 + 1, 后面一样

【代码】:
```
#include

using namespace std;

const int maxn = 1e5 + 10;

const int mod = 142857;

int t,n,m,k,x,u,v,num,ans;

vector G[maxn];

int inDeg[maxn];

int sum[maxn];

queue q;

int topSort()

{

num=0;

while(!q.empty()) q.pop();

for(int i=1;i<=n;i++) if(!inDeg[i]) q.push(i);

while(!q.empty())

{

num++;

int now = q.front();

q.pop();

for(int i=0;i<G[now].size();i++)

{

int nxt = G[now][i];

if(--inDeg[nxt] == 0)

{

q.push(nxt);

sum[nxt]=sum[now]+1;

}

}

}

if(num != n) return -1;

int ans=0;

for(int i=1;i<=n;i++)

ans+=sum[i]+888;

return ans;

}

int main()

{

while(~scanf("%d%d",&n,&m))

{

memset(inDeg,0,sizeof(inDeg));

memset(sum,0,sizeof(sum));

for(int i=1;i<=n;i++) G[i].clear();

while(m--)

{

scanf("%d%d",&u,&v);

G[v].push_back(u); //反向建边

inDeg[u]++;

}

printf("%d\n",topSort());

}

}

HDU 2647 Reward【反向拓扑排序】的更多相关文章

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

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

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

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

  3. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  4. HDU 2647 Reward(拓扑排序,vector实现邻接表)

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

  5. hdu 2647 Reward(拓扑排序,反着来)

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

  6. HDU 4857 (反向拓扑排序 + 优先队列)

    题意:有N个人,M个优先级a,b表示a优先于b.而且每一个人有个编号的优先级.输出顺序. 思路来自:与PKU3687一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...

  7. 杭电 2647 Reward (拓扑排序反着排)

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

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

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

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

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

  10. 逃生(HDU4857 + 反向拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...

随机推荐

  1. 【bzoj1180】[CROATIAN2009]OTOCI LCT

    题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes”,并且在结点 ...

  2. CF869E The Untended Antiquity 解题报告

    CF869E The Untended Antiquity 题目描述 \(\text{Adieu l'ami}\). Koyomi is helping Oshino, an acquaintance ...

  3. BEGIN TRAN;

    USE master Create Database TestDb on Primary ( name='TestDb_data', filename='G:\TempData\Db\TestDb_d ...

  4. The 13th Zhejiang Provincial Collegiate Programming Contest - I

    People Counting Time Limit: 2 Seconds      Memory Limit: 65536 KB In a BG (dinner gathering) for ZJU ...

  5. hive连接数

    使用hive分析日志作业很多的时候,需要修改mysql的默认连接数 修改方法   打开/etc/my.cnf文件 在[mysqld]  中添加 max_connections=1000 重启mysql ...

  6. Spring 4 + Hibernate 4 下 getCurrentSession()的使用情况

    前言:1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会.   2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭 ...

  7. 图论---图的m-点着色判定问题(回溯法--迭代式)

    转自 图的m着色问题 图的m-着色判定问题——给定无向连通图G和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色,是否有一种着色法使G中任意相邻的2个顶点着不同颜色? 图的m-着色优化 ...

  8. Spring - IoC(10): 生命周期

    Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建.销毁.管理 Bean 生命周期行为主要有两个时机: 注入 Bean 的依赖关系之后 即 ...

  9. Ueditor 1.4.3 插入表格后无边框无颜色,不能正常显示

    在使用Ueditor 插入表格的功能时,发现插入时正常. 但保存到后台后再取出来,表格不能正常显示.查看保存的html代码,发现保存时并未给table 添加border属性.以致于再次取出来时,不能正 ...

  10. javascript的阻塞机制

    javascript的阻塞机制 浏览器在执行javascript代码时,不能同时做其它事情,当遇到javascript时,浏览器会下载js文件,解析并执行该文件,而在这期间页面的渲染是完全被阻塞的,因 ...