POJ2987 Firing


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 ≤ i ≤ n). The remaining m lines each contain two integers i and j (1 ≤ i, j ≤ n) 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

5 5

8

-9

-20

12

-10

1 2

2 5

1 4

3 4

4 5

Sample Output

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.


题意就是有一些员工,开除一个员工会得到相应的盈利或者亏损

然后员工之间有一些上司和下属的关系,如果开除一个上司必须要开除他的所有下属

最大化盈利,输出开除的员工数和盈利数

一开始不知道这是最大权闭合图,后面看到网上的介绍才了解。

那么什么是闭合图呢?

在一个图中,我们选取一些点构成集合,且集合中的点向外连出的弧所指向的终点也在集合中,则我们称这个集合为闭合图

那么最大权闭合图就是所有闭合图中权值和最大的一个

了解了概念之后,我们如何对这一类问题进行解决呢?

我们构造一个流量网络满足以下条件:

1 存在源点S和汇点T

2 对于所有权值为正的点,连接一条容量为点权,从S到这个点的边

3 对于所有权值为负的点,连接一条容量为点权相反数,从这个点到T的边

4 原图中连接两点的边,在流量网络中用容量为INF的边连接

在最大权闭合图中,有以下性质

1 最小割为简单割:割集的每条边都与S或T关联

2 最小割和S点关联的集合减去S点就是最大权闭合图

我们根据这两个性质就可以对问题进行求解了

感觉其实是到板子题


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define N 10010
struct Edge{
LL u,v,cap,flow;
Edge(LL xu,LL xv,LL xcap,LL xflow){
u=xu;v=xv;cap=xcap;flow=xflow;
}
};
struct Dinic{
LL s,t,vis[N],d[N];
vector<Edge> E;
vector<LL> G[N];
void add(LL u,LL v,LL cap){
E.push_back(Edge(u,v,cap,0));
E.push_back(Edge(v,u,0,0));
LL m=E.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
bool BFS(){
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
queue<LL> Q;
Q.push(s);
vis[s]=1;
while(!Q.empty()){
LL u=Q.front();Q.pop();
for(LL i=0;i<G[u].size();i++){
Edge &e=E[G[u][i]];
if(!vis[e.v]&&e.cap>e.flow){
vis[e.v]=1;
d[e.v]=d[u]+1;
Q.push(e.v);
}
}
}
return vis[t];
}
LL DFS(LL u,LL a){
if(u==t||!a)return a;
LL flow=0;
for(LL i=0;i<G[u].size();i++){
Edge &e=E[G[u][i]];
if(d[e.v]!=d[u]+1)continue;
LL f=DFS(e.v,min(a,e.cap-e.flow));
e.flow+=f;
E[G[u][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a)break;
}
return flow;
}
LL Maxflow(){
LL flow=0;
while(BFS())flow+=DFS(s,INF);
return flow;
}
void search(LL u){
vis[u]=1;
for(LL i=0;i<G[u].size();i++){
Edge e=E[G[u][i]];
if(e.cap-e.flow>0&&!vis[e.v])search(e.v);
}
}
}dinic;
LL n,m,p,q,sum=0;
int main(){
scanf("%lld%lld",&n,&m);
dinic.s=0;dinic.t=n+1;
for(LL i=1;i<=n;i++){
scanf("%lld",&p);
if(p>0)dinic.add(0,i,p),sum+=p;
else dinic.add(i,n+1,-p);
}
for(LL i=1;i<=m;i++){
scanf("%lld%lld",&p,&q);
dinic.add(p,q,INF);
}
LL ans1=0,ans2=sum-dinic.Maxflow();
memset(dinic.vis,0,sizeof(dinic.vis));
dinic.search(dinic.s);
for(LL i=1;i<=n;i++)ans1+=dinic.vis[i];
printf("%lld %lld",ans1,ans2);
return 0;
}

POJ2987 Firing 【最大权闭合图】的更多相关文章

  1. POJ2987 Firing 最大权闭合图

    详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...

  2. poj2987 Firing 最大权闭合子图 边权有正有负

    /** 题目:poj2987 Firing 最大权闭合子图 边权有正有负 链接:http://poj.org/problem?id=2987 题意:由于金融危机,公司要裁员,如果裁了员工x,那么x的下 ...

  3. poj 2987 Firing 最大权闭合图

    题目链接:http://poj.org/problem?id=2987 You’ve finally got mad at “the world’s most stupid” employees of ...

  4. POJ 2987 Firing(最大流最小割の最大权闭合图)

    Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...

  5. POJ 2987:Firing(最大权闭合图)

    http://poj.org/problem?id=2987 题意:有公司要裁员,每裁一个人可以得到收益(有正有负),而且如果裁掉的这个人有党羽的话,必须将这个人的所有党羽都裁除,问最少的裁员人数是多 ...

  6. POJ 2987 Firing【最大权闭合图-最小割】

    题意:给出一个有向图,选择一个点,则要选择它的可以到达的所有节点.选择每个点有各自的利益或损失.求最大化的利益,以及此时选择人数的最小值. 算法:构造源点s汇点t,从s到每个正数点建边,容量为利益.每 ...

  7. POJ 2987 Firing 网络流 最大权闭合图

    http://poj.org/problem?id=2987 https://blog.csdn.net/u014686462/article/details/48533253 给一个闭合图,要求输出 ...

  8. POJ 2987 Firing(最大权闭合图)

    [题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...

  9. poj 2987 最大权闭合图

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

随机推荐

  1. Matlab 日期频次统计

    一.孕妇建档月份频次统计 源数据样本,为某医院一段时间内的孕妇建档时间 2015-04-22 10:12:522014-11-21 17:16:472013-12-16 17:35:442013-12 ...

  2. 分布式系统中的幂等性-zookeeper与dubbo

    现如今我们的系统大多拆分为分布式SOA,或者微服务,一套系统中包含了多个子系统服务,而一个子系统服务往往会去调用另一个服务,而服务调用服务无非就是使用RPC通信或者restful,既然是通信,那么就有 ...

  3. JQuery常用和很有用处的方法

    1.grep()方法: 用于数组元素过滤筛选 grep(array,callback,invert) array:待过滤数组; callback:处理数组中的每个元素,并过滤元素,该函数中包含两个参数 ...

  4. [spring]Bean注入——在XML中配置

    Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...

  5. pahlcon:cookies设置

    非加密方式(简单,但不推荐) 步骤 1 在全局容器中加入Cookie: $di->set('cookies', function () { $cookies = new \Phalcon\Htt ...

  6. Spring Cloud实战

    Spring Cloud实战(一)-Spring Cloud Config Server https://segmentfault.com/a/1190000006149891 https://seg ...

  7. 设计模式--模板方法模式C++实现

    模板方法模式C++实现 1定义 定义一个操作的算法的框架,而将一些步骤延迟到子类中.使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 实现方案:将算法/逻辑框架放在抽象基类中,并定义好 ...

  8. Log4j详细设置说明

    1. 动态的改变记录级别和策略,即修改log4j.properties,不需要重启Web应用,这需要在web.xml中设置一下.2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径 ...

  9. 《The Cg Tutorial》阅读笔记——凹凸贴图 Bump Mapping

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/5018103.html 凹凸贴图 Bump Mapping 一.简介 凹凸贴图用于在不 ...

  10. 利用有道翻译Api实现英文翻译功能

    有道翻译提供了翻译和查词的数据接口.通过数据接口,您可以获得一段文本的翻译结果或者查词结果.       通过调用有道翻译API数据接口,您可以在您的网站或应用中更灵活地定制翻译和查词功能. 第一步: ...