描述


http://poj.org/problem?id=2987

要炒员工鱿鱼,炒了一个人,他的下属一定被炒.给出每个人被炒后公司的收益(负值表示亏损),问怎样炒公司收益最大,以及这种方法炒了几个人.(先输出人数)

Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 9674   Accepted: 2906

Description

You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do some firings. You’re now simply too mad to give response to questions like “Don’t you think it is an even more stupid decision to have signed them?”, yet calm enough to consider the potential profit and loss from firing a good portion of them. While getting rid of an employee will save your wage and bonus expenditure on him, termination of a contract before expiration costs you funds for compensation. If you fire an employee, you also fire all his underlings and the underlings of his underlings and those underlings’ underlings’ underlings… An employee may serve in several departments and his (direct or indirect) underlings in one department may be his boss in another department. Is your firing plan ready now?

Input

The input starts with two integers n (0 < n ≤ 5000) and m (0 ≤ m ≤ 60000) on the same line. Next follows n + m lines. The first n lines of these give the net profit/loss from firing the i-th employee individually bi (|bi| ≤ 107, 1 ≤ in). The remaining m lines each contain two integers i and j (1 ≤ i, jn) meaning the i-th employee has the j-th employee as his direct underling.

Output

Output two integers separated by a single space: the minimum number of employees to fire to achieve the maximum profit, and the maximum profit.

Sample Input

  1. 5 5
  2. 8
  3. -9
  4. -20
  5. 12
  6. -10
  7. 1 2
  8. 2 5
  9. 1 4
  10. 3 4
  11. 4 5

Sample Output

  1. 2 2

Hint

As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.

Source

分析


求最大收益是用最大权闭合图.要炒一个人,他的下属也要被炒,那么边由上司连向下属,求最大权闭合图即可.

求炒了几个人实际是就是求先前求出来的那个最大权闭合图中点的个数.最大权闭合图对应的是最小割,而最小割中的边都是满流的,所以从源点出发无法到达闭合图的补集(即T),并且由于是闭合图,所以图中的边都不属于割,这样闭合图中就没有流量,从源点出发可以到达闭合图中的每一个点.

注意:

1.要用long long.

ps.f[S,T]=|f|,即整个图的流等于流过割的流,这样这道题中,f=fmax=cmin,又f=f[S,T],所以f[S,T]=cmin,也就是流过最小割的流等于最小割的容量,所以满流了.

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<queue>
  6. #define rep(i,n) for(int i=0;i<(n);i++)
  7. #define for1(i,a,n) for(int i=(a);i<=(n);i++)
  8. #define CC(i,a) memset(i,a,sizeof(i))
  9. #define read(a) a=getnum()
  10. #define ll long long
  11. using namespace std;
  12.  
  13. const int maxn=+,INF=0x7fffffff;
  14. int n,m;
  15. ll sumw;
  16. int level[maxn],iter[maxn];
  17. bool vis[maxn];
  18. struct edge
  19. {
  20. int to,cap,rev;
  21. edge() {}
  22. edge(int a,ll b,int c) : to(a),cap(b),rev(c) {}
  23. };
  24. vector <edge> g[maxn];
  25.  
  26. inline int getnum()
  27. {
  28. int r=,k=; char c;
  29. for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;
  30. for(;c>=''&&c<='';c=getchar()) r=r*+c-'';
  31. return r*k;
  32. }
  33.  
  34. void add_edge(int from,int to,int cap)
  35. {
  36. g[from].push_back(edge(to,cap,g[to].size()));
  37. g[to].push_back(edge(from,,g[from].size()-));
  38. }
  39.  
  40. void bfs(int s)
  41. {
  42. CC(level,-);
  43. level[s]=;
  44. queue <int> q;
  45. q.push(s);
  46. while(!q.empty())
  47. {
  48. int t=q.front(); q.pop();
  49. rep(i,g[t].size())
  50. {
  51. edge e=g[t][i];
  52. if(e.cap>&&level[e.to]<)
  53. {
  54. level[e.to]=level[t]+;
  55. q.push(e.to);
  56. }
  57. }
  58. }
  59. }
  60.  
  61. int dfs(int v,int t,int f)
  62. {
  63. if(v==t) return f;
  64. for(int &i=iter[v];i<g[v].size();i++)
  65. {
  66. edge &e=g[v][i];
  67. if(e.cap>&&level[e.to]>level[v])
  68. {
  69. int d=dfs(e.to,t,min(f,e.cap));
  70. if(d>)
  71. {
  72. e.cap-=d;
  73. g[e.to][e.rev].cap+=d;
  74. return d;
  75. }
  76. }
  77.  
  78. }
  79. return ;
  80. }
  81.  
  82. ll max_flow(int s,int t)
  83. {
  84. ll flow=;
  85. bfs(s);
  86. while(level[t]>)
  87. {
  88. int f;
  89. CC(iter,);
  90. while((f=dfs(s,t,INF))>) flow+=f;
  91. bfs(s);
  92. }
  93. return flow;
  94. }
  95.  
  96. int DFS(int v)
  97. {
  98. vis[v]=true;
  99. int ans=;
  100. rep(i,g[v].size())
  101. {
  102. edge e=g[v][i];
  103. if(!vis[e.to]&&e.cap>) ans+=DFS(e.to);
  104. }
  105. return ans;
  106. }
  107.  
  108. void init()
  109. {
  110. read(n); read(m);
  111. for1(i,,n)
  112. {
  113. int w; read(w);
  114. if(w>)
  115. {
  116. sumw+=w;
  117. add_edge(,i,w);
  118. }
  119. else
  120. {
  121. add_edge(i,n+,-w);
  122. }
  123. }
  124. for1(i,,m)
  125. {
  126. int a,b; read(a); read(b);
  127. add_edge(a,b,INF);
  128. }
  129. }
  130.  
  131. int main()
  132. {
  133. #ifndef ONLINE_JUDGE
  134. freopen("firing.in","r",stdin);
  135. freopen("firing.out","w",stdout);
  136. #endif
  137. init();
  138. ll ans=sumw-max_flow(,n+);
  139. printf("%d %lld\n",DFS()-,ans);
  140. #ifndef ONLINE_JUDGE
  141. fclose(stdin);
  142. fclose(stdout);
  143. system("firing.out");
  144. #endif
  145. return ;
  146. }
 

POJ_2987_Firing_(最大流+最大权闭合图)的更多相关文章

  1. 【TYVJ】1338 QQ农场(最大流+最大权闭合图)

    http://tyvj.cn/Problem_Show.aspx?id=1338 时间才排到rank7,还不快啊囧.isap我常数都写得那么小了... 最大权闭合图看我另一篇博文吧 此题很明显的模型. ...

  2. BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1565 n*m的矩阵,可以种植植物,僵尸从图的右边进入吃植物.前面的植物可以保护后面的植物,还有 ...

  3. BZOJ_1497_[NOI2006]_最大获利_(最大流+最大权闭合图)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 共n个站点,给出建立每个站点所需要的花费.现在有m个客户需要开通服务,每个客户需要有两个 ...

  4. [网络流24题] 太空飞行计划问题 (最大流->最大权闭合图)

    洛谷传送门 LOJ传送门 做这道题之前建议先看这篇论文,虽然论文里很多地方用了很多术语,但hbt神犇讲得很明白 这篇题解更加偏向于感性理解 把问题放到二分图上,左侧一列点是实验,权值为$p[i]$,右 ...

  5. [网络流24题] 方格取数问题/骑士共存问题 (最大流->最大权闭合图)

    洛谷传送门 LOJ传送门 和太空飞行计划问题一样,这依然是一道最大权闭合图问题 “骑士共存问题”是“方格取数问题”的弱化版,本题解不再赘述“骑士共存问题”的做法 分析题目,如果我们能把所有方格的数都给 ...

  6. [luoguP2762] 太空飞行计划问题(最大权闭合图—最小割—最大流)

    传送门 如果将每一个实验和其所对的仪器连一条有向边,那么原图就是一个dag图(有向无环) 每一个点都有一个点权,实验为收益(正数),仪器为花费(负数). 那么接下来可以引出闭合图的概念了. 闭合图是原 ...

  7. poj 2987 最大权闭合图

    Language: Default Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 8744   Accept ...

  8. 最大权闭合图 && 【BZOJ】1497: [NOI2006]最大获利

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 最大权闭合图详细请看胡伯涛论文<最小割模型在信息学竞赛中的应用>,我在这里截图它的 ...

  9. 最大权闭合图hdu3996

    定义:最大权闭合图:是有向图的一个点集,且该点集的所有出边都指向该集合.即闭合图内任意点的集合也在改闭合图内,给每个点分配一个点权值Pu,最大权闭合图就是使闭合图的点权之和最大. 最小割建边方式:源点 ...

随机推荐

  1. Java分布式处理技术(RMI,JDNI)

    http://hedaoyuan.blog.51cto.com/4639772/813702 1.1 RMI的基本概念 1.1.1 什么是RMI RMI(Remote Method Invocatio ...

  2. HDU 3127 WHUgirls(DP 完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3127 题目大意:将一块长x宽y的矩形布料,剪成小的矩形(每个给定的小矩形都对应一个价值),使得所有小矩 ...

  3. std::map的操作:插入、修改、删除和遍历

    using namespace std; std::map<int,int> m_map; 1.添加 for(int i=0;i<10;i++) { m_map.insert(mak ...

  4. 第16章 网络IPC:套接字总结

    1 套接字是通信端点的抽象 创建套接字: int socket(int domain,int type,int protocol) domain:通信域 AF_INET.AF_INET6.AF_LOC ...

  5. HBuilder手机Iphone运行提示“未受信用的企业级开发者”

    HBuilder手机Iphone运行提示“未受信用的企业级开发者” 解决方法:设置-----通用------设备管理-----点击Digtial Heaven....---信任"Digtia ...

  6. linux - 创建用户

    apt-get update apt-get upgrade root@iZ28t2p7lz9Z:~# adduser cuiAdding user `cui' ...Adding new group ...

  7. uboot下 Nand flash 启动 内核与根文件系统

    u-boot版本: u-boot-2010.03_tekkamanninja修改的u-boot 1.将uboot通过j-link烧写到norflash,启动后 saveenv 将参数保存到 nandf ...

  8. Spring-Boot-XML-Restful-Service

    http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-write-an-xml-rest-service ...

  9. 使用Fiddler提高前端工作效率 (实例篇)

    上篇中,我们对Fiddler Web Debugger有了简单的接触,也许你已经开始在用Fiddler进行HTTP相关的调试,在这一篇,我们将通过一个实例了解Fiddler的神奇魔法. 在我们前端开发 ...

  10. HTML5 改良的 input 元素的种类

    html5中增加改良的input 元素 . 在过去我们制作网页输入框,会用到不少JS验证,如今有了HTML5写这种效果已经没有那么麻烦了,下面我来给大家介绍两种HTML5的input的新增加的类型应用 ...