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是 ...
随机推荐
- JS组件系列——KnockoutJS用法
前言:出于某种原因,需要学习下Knockout.js,这个组件很早前听说过,但一直没尝试使用,这两天学习了下,觉得它真心不错,双向绑定的机制简直太爽了.今天打算结合bootstrapTable和Kno ...
- luigi学习7--running from command line
最简单去运行一个luigi task的方式是通过luigi命令行工具. 示例代码: # my_module.py, available in your sys.path import luigi cl ...
- spark性能调优:资源优化
在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要的参数,以及如何设置 ...
- Qt5 Addin 出现问题模块计算机类型“x64”与目标计算机类型“X86”冲突
Qt5 Addin 出现问题 怎样VS2013下安装Qt5的插件 http://jingyan.baidu.com/article/a948d65159d8890a2dcd2e84.html ...
- Android Socket通信
1.TCP: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- 15)Java &和&&
&,双目运算符:将两个表达式的值按二进制位展开,对应的位(bit)按值进行"与"运算,结果保留在该位上- 比如170&204对应二进制就是 1010101 ...
- php中的日期
1.在PHP中获取日期和时间 time()返回当前时间的 Unix 时间戳. getDate()返回日期/时间信息. gettimeofday()返回当前时间信息.date_sunrise()返回给定 ...
- 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...
- ios 异步处理耗时操作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_asy ...
- Python学习教程(learning Python)--1.4 Python数据处理基础
本节主要讨论数据操作及运算符等基础知识,熟悉C语言相关知识的读者请跳过此节. 在高级语言编程过程中,有了数据以后通常要对数据进行相应的数据处理,加.减.乘.除等基本运算,不难理解. 在Python里 ...