poj 2987 Firing 最大权闭合图
题目链接:http://poj.org/problem?id=2987
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?
题目描述:公司解雇员工,每个员工有一个权值,可正可负可为零(对公司而言等同于员工对公司的贡献力)。然后给出一些员工上下级的关系,如果解雇一个员工(比如经理主管之类的),那么他手下的员工都会被解雇,问对公司获利最大的解雇计划以及解雇员工人数。
算法分析:每个员工看作一个点,新增源点from和汇点to,from连边权值为正的点,边权为该权值;to连边权值为负的点,边权为权值的绝对值;员工之间的上下级也连边,边权为无穷大。最后最大权闭合图=权值为正的点的权值的和-最小割。
最大权闭合图参考资料:
大牛博客:【网络流】最大权闭合图
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
typedef long long LL;
const int maxn=+;
const int M = +; int n,m,from,to;
struct node
{
int v,flow;
int next;
}edge[M*];
int head[maxn],edgenum; void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow ;
edge[edgenum].next=head[u] ;head[u]=edgenum++ ; edge[edgenum].v=u ;edge[edgenum].flow= ;
edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
} int d[maxn];
int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+ && edge[i].flow)
{
int x=dfs(v,min(edge[i].flow,cap));
edge[i].flow -= x;
edge[i^].flow += x;
cap -= x;
if (cap==) return flow;
}
}
return flow-cap;
} LL dinic()
{
LL ans=;
while (bfs()) ans += dfs(from,inf);
return ans;
} int vis[maxn];
void dfs2(int u)
{
if (u==to) return ;
vis[u]=;
for (int i=head[u] ;i!=- ;i=edge[i].next)
if (edge[i].flow> && !vis[edge[i].v ])
dfs2(edge[i].v);
} int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
from=n+;
to=from+;
int a,b;
LL sum=;
for (int i= ;i<=n ;i++)
{
scanf("%d",&a);
if (a>) {sum += a ;add(from,i,a) ;}
else add(i,to,-a);
}
for (int i= ;i<m ;i++)
{
scanf("%d%d",&a,&b);
add(a,b,inf);
}
sum=sum-dinic();
memset(vis,,sizeof(vis));
dfs2(from);
int ans=;
for (int i= ;i<=n ;i++) if (vis[i]) ans++;
printf("%d %I64d\n",ans,sum);
}
return ;
}
poj 2987 Firing 最大权闭合图的更多相关文章
- POJ 2987 - Firing - [最大权闭合子图]
题目链接:http://poj.org/problem?id=2987 Time Limit: 5000MS Memory Limit: 131072K Description You’ve fina ...
- POJ 2987 Firing | 最大权闭合团
一个点带权的图,有一些指向关系,删掉一个点他指向的点也不能留下,问子图最大权值 题解: 这是最大权闭合团问题 闭合团:集合内所有点出边指向的点都在集合内 构图方法 1.S到权值为正的点,容量为权值 2 ...
- POJ2987 Firing 最大权闭合图
详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...
- POJ 2987:Firing(最大权闭合图)
http://poj.org/problem?id=2987 题意:有公司要裁员,每裁一个人可以得到收益(有正有负),而且如果裁掉的这个人有党羽的话,必须将这个人的所有党羽都裁除,问最少的裁员人数是多 ...
- POJ 2987 Firing 网络流 最大权闭合图
http://poj.org/problem?id=2987 https://blog.csdn.net/u014686462/article/details/48533253 给一个闭合图,要求输出 ...
- POJ 2987 Firing(最大权闭合图)
[题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...
- POJ 2987 Firing(最大流最小割の最大权闭合图)
Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...
- POJ 2987 Firing【最大权闭合图-最小割】
题意:给出一个有向图,选择一个点,则要选择它的可以到达的所有节点.选择每个点有各自的利益或损失.求最大化的利益,以及此时选择人数的最小值. 算法:构造源点s汇点t,从s到每个正数点建边,容量为利益.每 ...
- poj 2987(最大权闭合图+割边最少)
题目链接:http://poj.org/problem?id=2987 思路:标准的最大权闭合图,构图:从源点s向每个正收益点连边,容量为收益:从每个负收益点向汇点t连边,容量为收益的相反数:对于i是 ...
随机推荐
- HTTP协议的URI及各种方法
每个Web服务器资源都有一个名字,这样客户端就可以说明他们感兴趣的资源是什么了,服务器资 源名被统称为:统一资源标识符(Uniform Resource Identifier, URI) Joe的五金 ...
- Centos 7.1+CDH5.7.2全部署流程
前期准备: JDK环境 版本:jdk-8u101-linux-x64.rpm 下载地址:oracle官网 mysql rpm包:http://dev.mysql.com/get/Downloads/M ...
- js给php传值
//ajax传值 var str= JSON.stringify(arr1);//数组转string //alert(typeof(str)); $.ajax({ url:'test.php' ,ty ...
- 常用按键ASCII码
ESC 27回车 13TAB 9Caps Lock 20Shift $10 Ctrl 17Alt 18空格 VK_SPACE 32退格 VK_BACK 8左徽标 VK_LWIN 91右徽标 VK_RW ...
- 实战Django:官方实例Part1
[写在前面] 撰写这个实战系列的Django文章,是很久之前就有的想法,问题是手头实例太少,一旦开讲,恐有"无米下锅"之忧. 随着对Django学习的深入,渐渐有了些心得,把这些心 ...
- 几道华为经典C语言面试题
1.找错 void test1() { char string[10]; char* str1="0123456789"; strcpy(string, str1); } 这里st ...
- C高级 服务器内核分析和构建 (一)
引言 最经看cloud wind 的 skynet服务器设计. 觉得特别精妙. 想来个专题先剖析其通信层服务器内核 的设计原理. 最后再优化.本文是这个小专题的第一部分, 重点会讲解对于不同平台通信基 ...
- 关于生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法
System.Drawing.Image OldImage = null; oldImage = System.Drawing.Image.FromFile(ImageUrl); 使用该方法读取图片时 ...
- SQL Server基本操作积累
一.基本操作 1.将数据绑定到DataGridVirw控件上显示的数据列标题将会是数据库中的字段名称,可以在使用select语句时使用AS关键字将转化为列名的别名 select name AS 姓名 ...
- Linux开机启动程序详解
Linux开机启动程序详解我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. ...