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.
译文:蒲公英的叔叔是工厂的老板。随着春节的到来,他想分发奖励给他的工人。现在他在分配奖励方面遇到了麻烦。工人们会比较他们的奖励,有些人可能会要求分配奖励,就像a's奖励超过b's一样.Dandelion的不克不及要满足所有的要求,当然他想用最少的钱。每件作品的奖励将至少为888,因为这是一个幸运数字。

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.
译文:一行有两个整数n和m,代表作品数量和需求数量(n <= 10000,m <= 20000),
然后是m行,每行包含两个整数a和b,代表a的奖励应该是比b要多。

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.
译文:对于每一种情况,打印蒲公英的叔叔需要分发的最少钱。如果不能满足所有作品的需求,则打印-1。

Sample Input

2 1
1 2
2 2
1 2
2 1

Sample Output

1777
-1
解题思路:拓扑排序。因为前者奖励比后者多,又要求用最少的钱来发放奖励,所以只取前者比后者多1的奖励金。如果按之前一般思维来做,即将奖金多的指向奖金少的,在拓扑奖金多的这个节点编号时,势必会影响到之前已经拓扑的节点编号的奖励,因为有可能比它奖励还要多1的节点,这样就不好处理之前已经拓扑的节点编号的奖励。因此,这题需要反过来思考,将奖励少的节点编号指向奖励多的节点编号,这样拓扑奖励少的时候,奖励多的只需比奖励少的多1,最终将所有奖励相加再加上888*n即可。题目给出的数据太大,为避免超时,采用邻接表的做法。对于此题的讲解还可以看看这篇博文:hdu 2647 Reward【拓扑排序】
AC代码:
 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int> vec[maxn];//邻接表,每个节点保存与它相连的边的另一个端点
queue<int> que;
int n,m,u,v,InDeg[maxn],cnt[maxn];//记录每个节点的入度,cnt记录每个节点该得到的奖励
bool topsort(){
int num=;
for(int i=;i<=n;++i)if(!InDeg[i])que.push(i);//预处理,先将入度为0的节点编号入队
while(!que.empty()){
int now=que.front();que.pop();num++;
for(unsigned int i=;i<vec[now].size();++i){
if(--InDeg[vec[now][i]]==)que.push(vec[now][i]);
cnt[vec[now][i]]=cnt[now]+;//只需比出队元素奖励多1即可
}
}
if(num==n)return true;//满足条件的话返回1
else return false;
}
int main()
{
while(cin>>n>>m){
for(int i=;i<=n;++i)vec[i].clear();//全部清空
memset(InDeg,,sizeof(InDeg));//全部顶点的度清0
memset(cnt,,sizeof(cnt));
while(m--){
cin>>u>>v;
vec[v].push_back(u);//v指向u
InDeg[u]++;//u的入度加1
}
if(topsort()){
int sum=;
for(int i=;i<=n;++i)sum+=cnt[i];
cout<<(sum+*n)<<endl;
}
else cout<<"-1"<<endl;
}
return ;
}

题解报告:hdu 2647 Reward(拓扑排序)的更多相关文章

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

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

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

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

  3. HDU 2647 Reward (拓扑排序)

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

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

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

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

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

  6. HDU 2647 逆向拓扑排序

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

  7. 2021.07.17 题解 CF1385E Directing Edges(拓扑排序)

    2021.07.17 题解 CF1385E Directing Edges(拓扑排序) CF1385E Directing Edges - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) ...

  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 ...

随机推荐

  1. Django-REST-Framework JWT 实现SSO认证(上)

    一.什么是Django-REST-Framework? Django-REST-framework 是基于Django框架的一个web RESTful风格开发的框架,它可以实现API接口的快速开发,但 ...

  2. CentOS 安装Oracle 11g R2

    CentOS 安装Oracle 11g R2 学习了-/ https://www.osyunwei.com/archives/5445.html

  3. Crashing Robots POJ 2632 简单模拟

    Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...

  4. @Autowired中无法注入RestTemplate的问题

    1.在启动类中添加 @Beanpublic RestTemplate restTemplate(){ return new RestTemplate();} 即可解决无法注入RestTemplate的 ...

  5. POJ 1970 The Game

    The Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6886   Accepted: 1763 Descript ...

  6. endWith is not a function

    解决方法,增加String的扩展 String.prototype.endWith = function(suffix) { return this.indexOf(suffix, this.leng ...

  7. nodejs 运行

    运行 Node.js 程序的基本方法就是执行 node script.js,其中 script.js①是脚本的文件名. 除了直接运行脚本文件外,node --help 显示的使用方法中说明了另一种输出 ...

  8. 再次讨论javascript 中的this

    原文: http://www.jb51.net/article/77519.htm 核心总结: 1.不论函数在哪里被调用,只要没有指定调用方,则this都指向window.指定了调用方,就指向调用方. ...

  9. 「五」创建一个带 tomcat 服务的基础镜像(修订版)

    Tomcat Tomcat 简单介绍 Tomcat server是一个免费的开放源码的Web 应用server,属于轻量级应用server.在中小型系统和并发訪问用户不是非常多的场合下被普遍使用,是开 ...

  10. react State改变,页面却没有改变

    react 小白编程 做项目时遇到了个问题,无论我怎么查看我的action.reducer 还是 dispatch 函数,都没有发现有什么毛病.但是 debugger 的时候,state 改变了,页面 ...