题意不说了,说下思路。

给出的关系是a要求的工资要比b的工资多,因为尽可能的让老板少付钱,那么a的工资就是b的工资+1。能够确定关系为a>b,依据拓扑排序建边的原则是把“小于”关系看成有向边。那么我们能够建边v->u。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <math.h>
  8. #include <map>
  9. #include <queue>
  10. #include <stack>
  11. #include <set>
  12. #define M 10000+5
  13. #define LL long long
  14. #define Ld __int64
  15. #define eps 0.00001
  16. #define INF 999999999
  17. #define MOD 112233
  18. #define MAX 26
  19. #define PI acos(-1.0)
  20. using namespace std;
  21.  
  22. vector<int> G[M];
  23. int num[M];
  24. int n;
  25. int into[M],ans;
  26.  
  27. int toposort()
  28. {
  29. queue<int> q;
  30. int cnt=0;
  31. ans=0;
  32. for(int i=1;i<=n;i++)
  33. {
  34. if(into[i]==0) //入度为0的点进入队列中
  35. q.push(i);
  36. }
  37. while(!q.empty())
  38. {
  39. cnt++;
  40. int u=q.front();
  41. ans+=num[u];
  42. q.pop();
  43. for(int i=0;i<G[u].size();i++)
  44. {
  45. int v=G[u][i];
  46. if(--into[v]==0) //若删除(u,v)这条边之后,v的入度也为0,则压入队列中
  47. {
  48. q.push(v);
  49. num[v]=num[u]+1; //v要求的工资比u高
  50. }
  51. }
  52. }
  53. if(cnt!=n) //推断有无环。-1表示有环。1表示无环
  54. ans=-1;
  55. return ans;
  56. }
  57.  
  58. int main()
  59. {
  60. int m;
  61. while(~scanf("%d%d",&n,&m))
  62. {
  63. for(int i=0;i<=n;i++)
  64. {
  65. G[i].clear();
  66. into[i]=0;
  67. num[i]=888;
  68. }
  69. while(m--)
  70. {
  71. int u,v;
  72. scanf("%d%d",&u,&v);
  73. G[v].push_back(u);
  74. into[u]++;
  75. }
  76. printf("%d\n",toposort());
  77. }
  78. return 0;
  79. }
  80.  
  81. /*
  82.  
  83. 5 4
  84. 2 1
  85. 2 5
  86. 5 3
  87. 3 4
  88.  
  89. */

HDU2647(拓扑排序+反向建图)的更多相关文章

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

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

  2. HDU 4857 逃生 【拓扑排序+反向建图+优先队列】

    逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  3. hdoj--4857--逃生(拓扑排序+反向建图)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  4. Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图

    E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. hdu 4857 逃生 拓扑排序+逆向建图

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...

  6. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  7. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  8. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  9. 逃生 HDU 4857(反向建图 + 拓扑排序)

    逃生 链接 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必 ...

随机推荐

  1. 行为驱动开发(BDD)

    行为驱动开发(BDD) 引言 BDD是对TDD理念的扩展.BDD强调有利害关系的技术团体和非技术团队都要参与到软件开发过程中.可以把它看成一种强调团体间合作的敏捷方法.大多数采用某种敏捷方法的团队最终 ...

  2. php文件操作基本使用方法

    <?php /* $fp=fopen("tmp.html","r"); $str=fread($fp,filesize("tmp.html&qu ...

  3. PHP读取Excel里的文件

    下载phpExcelReader  http://sourceforge.net/projects/phpexcelreader 解压后得到以下这些文件 jxlrwtest.xls这个excel文件有 ...

  4. 四个漂亮CSS样式表

    1. 单像素的边框CSS表格 这是一个非常所用的表格风格. 源码: <!-- CSS goes in the document HEAD or added to your external st ...

  5. windows中间vmware的Linux系统安装jdk步骤

    1.设置文件的享受,对于本地阅读windows档 于vmware虚拟机设置共享文件夹,那么共享文件中,你可以 2.然后打开虚拟机上,使用root输入账户,然后,在夹/mnt/hgfs/   共享文件了 ...

  6. Directx11学习笔记【十八】 Blending混合

    本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5634580.html 在d3d11中是按frame来渲染物体的,在 ...

  7. java多线程Future和Callable类的解释与使用

    一,描写叙述 ​在多线程下编程的时候.大家可能会遇到一种需求,就是我想在我开启的线程都结束时,同一时候获取每一个线程中返回的数据然后再做统一处理,在这种需求下,Future与Callable的组合就派 ...

  8. .Net下一个Winform方案可以让MessageBox.Show它显示在父窗口的中间

    下面的文字,缺省值是在屏幕中间显示. DialogResult dr = MessageBox.Show("是否要删除此数据?", "删除确认", Messag ...

  9. Zygote过程【3】——SystemServer诞生

    欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 在ZygoteInit的main()方法中做了几件大事.当中一件便是启动Systemserver进程.代码例如以下: ...

  10. 移动web:翻页场景动画

    在移动web,特别是在微信中,经常看到一种翻页动画效果,也称为场景动画. 一页一页的翻过,像在看书,每页的内容以各种"炫酷"的效果出现在你的眼里,配上一首动听的音乐,你有没有喜欢上 ...