【题目链接】 http://poj.org/problem?id=2987

【题目大意】

  为了使得公司效率最高,因此需要进行裁员,
  裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果,
  如果裁去一个上级,那么他所管辖的下级需要全部裁掉,问最大效率提升
  同时求出最小裁员

【题解】

  我们从上司向所有的下属连线会发现裁去的部分恰是一个最大权闭合图
  所以按照最大权闭合图建图求最小割,残余网络就是裁员数量。

【代码】

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAX_V=5010;
typedef long long LL;
struct edge{int to;LL cap;int rev;};
vector<edge> G[MAX_V];
int V,level[MAX_V],iter[MAX_V];
void add_edge(int from,int to,int cap){
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,0,G[from].size()-1});
}
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front(); que.pop();
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}
LL dfs(int v,int t,LL f){
if(v==t)return f;
for(int &i=iter[v];i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&level[v]<level[e.to]){
LL d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}return 0;
}
LL max_flow(int s,int t){
LL flow=0;
for(;;){
bfs(s);
if(level[t]<0)return flow;
memset(iter,0,sizeof(iter));
LL f;
while((f=dfs(s,t,INF))>0){
flow+=f;
}
}
}
const int MAX_N=5000;
const int MAX_M=60000;
int N,M,w[MAX_N],a[MAX_M],b[MAX_M];
LL max_weight_closure(int s,int t){
LL W=0; V=t;
for(int i=0;i<=V;i++)G[i].clear();
for(int i=0;i<N;i++){
if(w[i]>0)W+=w[i],add_edge(s,i,w[i]);
if(w[i]<0)add_edge(i,t,-w[i]);
}
for(int i=0;i<M;i++){
add_edge(a[i]-1,b[i]-1,INF);
}
return W-max_flow(s,t);
}
int leftv,vis[MAX_V];
void cal_res_net(int v){
++leftv;
vis[v]=1;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&!vis[e.to])cal_res_net(e.to);
}
}
void init(){
for(int i=0;i<N;i++)scanf("%d",&w[i]);
for(int i=0;i<M;i++)scanf("%d%d",&a[i],&b[i]);
}
void solve(){
int s=N,t=N+1;
LL max_profit=max_weight_closure(s,t);
memset(vis,0,sizeof(vis));
leftv=0;
cal_res_net(s);
printf("%d %lld\n",--leftv,max_profit);
}
int main(){
while(~scanf("%d%d",&N,&M)){
init();
solve();
}return 0;
}

POJ 2987 Firing(最大权闭合图)的更多相关文章

  1. poj 2987 Firing 最大权闭合图

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

  2. POJ 2987 - Firing - [最大权闭合子图]

    题目链接:http://poj.org/problem?id=2987 Time Limit: 5000MS Memory Limit: 131072K Description You’ve fina ...

  3. POJ 2987 Firing | 最大权闭合团

    一个点带权的图,有一些指向关系,删掉一个点他指向的点也不能留下,问子图最大权值 题解: 这是最大权闭合团问题 闭合团:集合内所有点出边指向的点都在集合内 构图方法 1.S到权值为正的点,容量为权值 2 ...

  4. POJ2987 Firing 最大权闭合图

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

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

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

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

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

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

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

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

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

  9. poj 2987(最大权闭合图+割边最少)

    题目链接:http://poj.org/problem?id=2987 思路:标准的最大权闭合图,构图:从源点s向每个正收益点连边,容量为收益:从每个负收益点向汇点t连边,容量为收益的相反数:对于i是 ...

随机推荐

  1. appium 多个设备同时执行

    测试需要同时在多个android设备上运行,就需要启动多个appium 使用adb命令获取udid,命令:adb get-serialno 使用的是testng测试框架,代码使用java编写 第一台, ...

  2. Python学习5,三级菜单实例

    _author_ = "Happyboy" data = { '北京':{ "昌平":{ "沙河":["Happyboy" ...

  3. 项目中使用ECharts插件实现统计功能

    一.前端界面 // 界面中定义一个div,放图表 <div id="box" style="width: 600px;height:400px;padding: 1 ...

  4. python-成员修饰符

    python的面相对象中,拥有3个成员,字段.方法.属性 class Foo: def __init__(self,name): #公有字段name在类中与类外均能调用 self.name = nam ...

  5. Android之SeekBar总结(一)

    2015-04-24 SeekBar: 一种特殊的进度条,包含一个滑块用于调节进度值. API 中目录结构如下: 包含几种特殊的属性: 1: max:设置进度条的最大值 .对应方法:setMax(in ...

  6. VB.NET概述

    简介 Visual Basic .NET属Basic系语言,VB.NET是一门高级的编程语言,当然在Basic系语言中VB.NET也确实是迄今为止最强大的一门编程语言.Visual Basic .NE ...

  7. Nginx 战斗准备 —— 优化指南

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了!而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...

  8. Redis键管理

    Redis键管理 Redis 键命令用于管理 redis 的键. 语法 Redis 键命令的基本语法如下: redis > COMMAND KEY_NAME redis > SET w3c ...

  9. MyBatis 基本演示

    主配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration P ...

  10. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...