POJ 2987 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.
题目大意:有n个点,每个点有一个权值(可能为负数),有m条有向边,求一个闭合子图,要求在权值最大的情况下点最少。
PS:所谓闭合子图,即在一个图中,若选择了x,那么x的所有后继都要选上(如a→b→c→d→e,选了b,就要选c、d、e)。
思路:最大权闭合图的经典建图:源点S到所有正权的点连一条边,容量为权值,从所有负权的点连一条边到汇点T,容量为权值的绝对值。对每一条边(i, j),连一条边i→j,容量为无穷大。所有正权权值之和减去最大流(最小割)为最大权,从S遍历一遍残量网络,能遍历到的点都为闭合子图中的点,计数即可得到答案。
小证明:在残量网络中,S能遍历到的点都为所选闭合子图,记为V(s),其余记为V(T)。那么V(S)中任何一个点,它都不会有容量为无穷大的边会指向V(T)中的点,因为若有,这条边要算入最小割,而最小割不可能为无穷大,这就保证了选了点x,x的后继都会被选上。记sum为所有正权权值之和,一个简单割(不割无穷大的边)割掉的与S关联的边的容量和为cut(S),割掉的与T关联的边的容量和为cut(T),对于所有选上的点,与源点关联的点的权和应该为sum - cut(S),cut(S)都是没有选上的点;与汇点关联的选上的点的权和为cut(T),cut(T)都是选上的点。那么该子图的权和为sum - cut(S) - cut(T),即sum - (cut(S) + cut(T)),我们需要最大化前面的式子,而sum是固定值,所以需要最小化cut(S) + cut(T),这个值最小的时候,就是最小割。
关于这样选到的点一定是最少的证明可以看下面的链接,不过简单地说就是:若有多种选择,那么就有一片点,选上它们之后权值总和为0,也就是源点到它们的容量应该是满的,因为我们是在残量网络中遍历,所以它们绝对不会被遍历到。至于下面链接所说的原图是DAG,这个我没看出来哪里说了……不过我们可以把环缩成一个点,不影响证明……
http://hi.baidu.com/dispossessed/item/f75d32161ae2d8573a176e8d
代码(391MS):
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; typedef long long LL; const int MAXN = ;
const int MAXE = ;
const LL INF = 0x3fff3fff3fff3fffLL; struct SAP {
int head[MAXN], dis[MAXN], gap[MAXN], cur[MAXN], pre[MAXN];
int to[MAXE], next[MAXE];
LL flow[MAXE];
int ecnt, n, st, ed; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, LL c) {
to[ecnt] = v; flow[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
//printf("%d->%d %I64d\n", u, v, c);
} void bfs() {
memset(dis, 0x3f, sizeof(dis));
queue<int> que; que.push(ed);
dis[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
++gap[dis[u]];
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p ^ ] && dis[v] > n) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} LL Max_flow(int ss, int tt, int nn) {
st = ss; ed = tt; n = nn;
LL ans = , minFlow = INF;
for(int i = ; i <= n; ++i) {
cur[i] = head[i];
gap[i] = ;
}
int u = pre[st] = st;
bfs();
while(dis[st] < n) {
bool flag = false;
for(int &p = cur[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[u] == dis[v] + ) {
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed) {
ans += minFlow;
while(u != st) {
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow;
}
minFlow = INF;
}
break;
}
}
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[v] < minDis) {
minDis = dis[v];
cur[u] = p;
}
}
if(--gap[dis[u]] == ) break;
++gap[dis[u] = minDis + ];
u = pre[u];
}
return ans;
} int cnt_S() {
memset(dis, , sizeof(dis));
int * vis = dis, ret = ;
queue<int> que; que.push(st);
vis[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && !vis[v]) {
vis[v] = ;
++ret;
que.push(v);
}
}
}
return ret;
}
} G; int main() {
int n, m, a, b;
LL x, sum = ;
scanf("%d%d", &n, &m);
int ss = , tt = n + ;
G.init();
for(int i = ; i <= n; ++i) {
scanf("%I64d", &x);
if(x > ) sum += x, G.add_edge(ss, i, x);
if(x < ) G.add_edge(i, tt, -x);
}
for(int i = ; i <= m; ++i) {
scanf("%d%d", &a, &b);
G.add_edge(a, b, INF);
}
LL ans1 = sum - G.Max_flow(ss, tt, tt);
int ans2 = G.cnt_S();
printf("%d %I64d\n", ans2, ans1);
}
POJ 2987 Firing(最大流最小割の最大权闭合图)的更多相关文章
- 【BZOJ-3438】小M的作物 最小割 + 最大权闭合图
3438: 小M的作物 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 825 Solved: 368[Submit][Status][Discuss ...
- 【POJ 2987】Firing (最小割-最大权闭合子图)
裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...
- POJ 2987 Firing【最大权闭合图-最小割】
题意:给出一个有向图,选择一个点,则要选择它的可以到达的所有节点.选择每个点有各自的利益或损失.求最大化的利益,以及此时选择人数的最小值. 算法:构造源点s汇点t,从s到每个正数点建边,容量为利益.每 ...
- poj 2987 Firing 最大权闭合图
题目链接:http://poj.org/problem?id=2987 You’ve finally got mad at “the world’s most stupid” employees of ...
- POJ 2987:Firing(最大权闭合图)
http://poj.org/problem?id=2987 题意:有公司要裁员,每裁一个人可以得到收益(有正有负),而且如果裁掉的这个人有党羽的话,必须将这个人的所有党羽都裁除,问最少的裁员人数是多 ...
- 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(最大权闭合图+割边最少)
题目链接:http://poj.org/problem?id=2987 思路:标准的最大权闭合图,构图:从源点s向每个正收益点连边,容量为收益:从每个负收益点向汇点t连边,容量为收益的相反数:对于i是 ...
- poj 2987 最大权闭合图
Language: Default Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8744 Accept ...
随机推荐
- 『ACM C++』 PTA 天梯赛练习集L1 | 050-51
加油加油,努力刷题 ------------------------------------------------L1-050------------------------------------ ...
- ubuntu包管理机制
1 ubuntu包管理机制 跟大家分享一下ubuntu的软件管理机制.如果你们有过: apt-get install 或者 apt-get update 失败的经历. 在众多的apt命令中迷失. 疑惑 ...
- 复习宝典之Redis
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第八章:Redis Redis是一个key-value的nosql数据库.先存到内存中,会根据一定的策略持久化到磁盘,即使断电也不会丢失 ...
- php 当不确定用户输入的是浮点 还是整数 还是字符串时
$price = (floatval($price))?intval(floatval($price)*100)/100:0;
- excel批量转换为CSV格式,xls批量导出csv格式
工具/原料 excel 2013 地址链接:http://pan.baidu.com/s/1c1ZABlu 密码:d3rc 方法/步骤 首选我们把需要导出为CVS的Excel文件整理集中到 ...
- Python模拟校园网登录
最近忙着实验室的项目,学习的时间相对较少.前一段时间刚开始接触python时,依葫芦画瓢照着写了一个爬虫,爬取了某个网站的图片.当看到一张张图片自动出现在电脑屏幕上时,有些小小成就感.我想大多数人开始 ...
- double工具类
package com.zq.utils; /** * * 经度数字操作类 * * Created by MyEclipse. Author: ChenBin E-mail: chenbin_2008 ...
- vscode vue 项目保存运行lint进行代码修正
{ "editor.tabSize": 2, "files.associations": { "*.vue": "vue" ...
- Python支付接口汇总大全(包含微信、支付宝等)
微信接口 wzhifuSDK- 由微信支付SDK 官方PHP Demo移植而来,v3.37下载地址 weixin_pay- 是一个简单的微信支付的接口 weixin_pay- 微信支付接口(V3.3. ...
- [Doctrine Migrations] 数据库迁移组件的深入解析一:安装与使用
场景分析 团队开发中,每个开发人员对于数据库都修改都必须手动记录,上线时需要人工整理,运维成本极高.而且在多个开发者之间数据结构同步也是很大的问题.Doctrine Migrations组件把数据库变 ...