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. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  2. [luoguP2486] [SDOI2011]染色(树链剖分)

    传送门 就是个模板啦 记录每一个点的左端点颜色和右端点颜色和当前端点颜色段数. 合并时如果左孩子右端点和右孩子左端点不同就 ans-- 在重链上跳的时候别忘记统计一下 ——代码 #include &l ...

  3. CodeForces 367E Sereja and Intervals

    CodeForces 3 67E (109 + 7). Two ways are considered distinct if there is such j(1 ≤ j ≤ n), that the ...

  4. Elasticsearch使用总结

    原文出自:https://www.2cto.com/database/201612/580142.html ELK干货:http://www.cnblogs.com/xing901022/p/4704 ...

  5. MySQL基于域名做高可用切换(Consul初试)

    一,Consul功能介绍 服务发现 - Consul的客户端可用提供一个服务,比如 api 或者mysql ,另外一些客户端可用使用Consul去发现一个指定服务的提供者.通过DNS或者HTTP应用程 ...

  6. 源码分析-react3-创建dom

    React.createElement class Welcome extends React.Component { constructor(){ super() this.state={ test ...

  7. vue2源码浏览分析01

    1.构造函数  Vue$3 function Vue$3 (options) { if ("development" !== 'production' && !(t ...

  8. 将list转为json字符串

    //确保JSP和servlet的编码方式一致 resp.setContentType("text/html;charset=GBK"); List<String> jy ...

  9. hp 1810-24g switch reset

    Specific steps to execute the factory default reset on the switch are: 1. Using a small, thin tool w ...

  10. 1.4 - OSPF的运行模式⑦

    帧中继的子接口选用原则: 1.在一个封装FR的物理接口上,可以同时承载多条PVC. 为了网络的可扩展性,建议不论在考试环境还是在工程环境中,都应该优先考虑使用子接口 2.应该创建几个子接口:在一个物理 ...