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
 
 
拓扑排序专题
vector 存图 注意vector的使用
算是拓扑排序的模板题目吧
多线程的  此题精妙在L数组 L初始为0

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> mp[20005];
  4. int n,m;
  5. int a,b;
  6. int in[20005];
  7. int L[20005];
  8. int flag;
  9. int re;
  10. struct node
  11. {
  12. int sum;
  13. };
  14. queue<node>q;
  15. struct node N,now;
  16. int main()
  17. {
  18. while(scanf("%d%d",&n,&m)!=EOF)
  19. {
  20. for(int i=1;i<=n;i++)
  21. mp[i].clear();
  22. memset(in,0,sizeof(in));
  23. memset(L,0,sizeof(L));
  24. for(int i=0;i<m;i++)
  25. {
  26. scanf("%d%d",&a,&b);
  27. mp[b].push_back(a);
  28. in[a]++;
  29. }
  30. for(int i=1;i<=n;i++)
  31. {
  32. if(in[i]==0)
  33. {
  34. N.sum=i;
  35. q.push(N);
  36. }
  37. }
  38. int jishu=n;
  39. int temp;
  40. while(!q.empty())
  41. {
  42. now=q.front();
  43. q.pop();
  44. jishu--;
  45. temp=L[now.sum];
  46. //cout<<now.sum<<endl;
  47. for(unsigned int i=0;i<mp[now.sum].size();i++)
  48. {
  49. if(--in[mp[now.sum][i]]==0)
  50. {
  51. N.sum=mp[now.sum][i];
  52. L[N.sum]=temp+1;
  53. q.push(N);
  54. }
  55. }
  56. }
  57. re=0;
  58. if(jishu>0)
  59. printf("-1\n");
  60. else
  61. {
  62. for(int i=1;i<=n;i++)
  63. re+=L[i];
  64. printf("%d\n",re+888*n);
  65. }
  66. }
  67. return 0;
  68. }

HDU2647 topsort的更多相关文章

  1. HDU2647

    第一道逆拓扑纪念一下... #include<iostream> #include<cstdio> #include<cstring> #include<cm ...

  2. 拓扑排序(topsort)

    本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...

  3. POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)

    题目大意: 为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的. 每一次Wind 选择两个房间  x 和 y,   让他的儿子从一个房间走到 ...

  4. poj1094 topsort

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32275   Accepted: 11 ...

  5. POJ - 3249 Test for Job (DAG+topsort)

    Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...

  6. 拓扑排序 topsort详解

    1.定义 对一个有向无环图G进行拓扑排序,是将G中所有顶点排成一个线性序列,通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列. 举例: h3 { marg ...

  7. poj 3648 2-SAT建图+topsort输出结果

    其实2-SAT类型题目的类型比较明确,基本模型差不多是对于n组对称的点,通过给出的限制条件建图连边,然后通过缩点和判断冲突来解决问题.要注意的是在topsort输出结果的时候,缩点后建图需要反向连边, ...

  8. 经典问题----拓扑排序(HDU2647)

    题目简介:有个工厂的老板给工人发奖金,每人基础都是888,工人们有自己的想法,如:a 工人想要比 b 工人的奖金高,老板想要使花的钱最少 那么就可以 给b 888,给a 889 ,但是如果在此基础上, ...

  9. 拓扑排序基础 hdu1258,hdu2647

    由这两题可知拓扑排序是通过“小于”关系加边建图的 hdu2647 /* 拓扑排序的原则是把“小于”看成有向边 此题反向建图即可 并且开num数组来记录每个点的应该得到的权值 */ #include&l ...

随机推荐

  1. Git命令使用大全

    一前言 最近公司在使用vue和WebAPI前后端分离的项目开发,使用的代码管理工具是git,刚开始使用的时候前端的vue文件还比较好处理,但是后端的C#文件在每一次自己编译之后上传都会和其他小伙伴的代 ...

  2. 返回json数组的GET接口

    Action() { web_reg_find("Search=Body", "SaveCount=find_cnt", "Text=code\&qu ...

  3. OpenMPI 集群配置

    现在有2台机器,希望可以尝试一下在多台机器上跑MPI的感觉,所以跑之前就得配置,先参考网址: https://www.cnblogs.com/awy-blog/p/3402949.html: 1. 配 ...

  4. 查找 二叉树中 k1 到 k2区间的节点

    vector<int> res; int key1, key2; void traverse(TreeNode * root){//采用前序遍历 if(root == NULL) retu ...

  5. 【Linux 运维】Centos7初始化网络配置

    设置网络 (1)动态获取一个IP地址 #dhclient        系统自动自动获取一个IP地址#ip addr         查看获取的ip地址(2)查看网关,子网掩码 虚拟机编辑>虚拟 ...

  6. 初步了解CUDA(零)

    初步了解CUDA,从历史开始,先不开发:

  7. NTP错误总结

    Ntp错误总结 解决ntp的错误 no server suitable for synchronization found 当用ntpdate -d 来查询时会发现导致 no server suita ...

  8. 第一次接触FPGA至今,总结的宝贵经验

    从大学时代第一次接触FPGA至今已有10多年的时间,至今记得当初第一次在EDA实验平台上完成数字秒表.抢答器.密码锁等实验时那个兴奋劲.当时由于没有接触到HDL硬件描述语言,设计都是在MAX+plus ...

  9. Python中from module import *语法

    from module import *的语法在Python 3.X和Python 2.X中的使用稍有区别: 在Python 3.X中,from module import *无法在函数里面使用,而在 ...

  10. 软工网络15团队作业4——Alpha阶段敏捷冲刺-1

    各个成员在 Alpha 阶段认领的任务 成员 Alpha 阶段认领的任务 肖世松 编写界面设计代码 杨泽斌 服务器连接与配置 叶文柠 数据库连接与配置 谢庆圆 编写功能板块代码 林伟航 编写功能板块代 ...