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 ;
}

最新文章

  1. spring 参数绑定
  2. CodeFirst实战:用文本数据库存档软件配置
  3. C和指针 第十一章 动态内存分配
  4. mysql 重置root密码
  5. Divide and Conquer:River Hopscotch(POJ 3258)
  6. 去掉inline-block元素默认间距的几种方法
  7. IOS中的动画菜单
  8. 最短路之Dijkstra算法
  9. css样式自适应,支持数字
  10. bzoj3876
  11. xyiyy开始写博客了
  12. 移动OA日程支持费用及评论
  13. Java虚拟机工作原理
  14. win server2012 r2 服务器共享文件夹设置
  15. 最简单易懂的Spring Security 身份认证流程讲解
  16. C#C/S框架演示 (MES系统)
  17. [MicroPython]TPYBoard v102炫彩跑马灯WS2812B
  18. 为laravel队列安装supervisor并配置
  19. Ping 不通的原因分析
  20. selenium_UI自动化——篇1(基础)

热门文章

  1. fillder抓取APP数据之小程序
  2. LA 3029 Subsequence
  3. Spring 工厂方法创建Bean 学习(三)
  4. hadoop full cluster 改为伪分布
  5. [luoguP1076] 寻宝(模拟)
  6. MTK平台系统稳定性分析
  7. Oracle操作管理之用户和角色
  8. 消息传递(cogs 1001)
  9. [Usaco2016 Open]Diamond Collector
  10. java 远程调用