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 ...
随机推荐
- python函数中闭包的概念说明
函数中闭包的概念说明 闭包: 内层函数对外层函数非全局变量的引用,就叫做闭包 判断闭包方法 ._closure_ : 执行后返回有效信息就是闭包,返回none就不是闭包 举例1: 是闭包 def wr ...
- 只查看xilong.txt[共100行]内第20行到第30行的内容
1: Test Environment [root@xilong startimes]# seq > xilong.txt [root@xilong startimes]# cat xilong ...
- 新手Linux命令学习
一.dd命令:1.可以复制文件,2.可以制作ios镜像,简单理解就是备份 常用的参数 if 设置输入文件的名称 of 设置输出文件的名称 bs 设置每个“”块“”大小 count 要复制“块” ...
- Python入门 —— 2048实战(字符界面和图形界面)
2048 game (共4种实现方法) 目录: .. 图形界面 ... pygame 和 numpy .. 字符界面 ... 第一种 ... curses ... wxpython ... 第二种 . ...
- Qt上FFTW組件的编译与安裝
Qt上FFTW組件的編譯安裝 FFTW是一個做頻譜非常實用的組件,本文講述在Windows和Linux兩個平臺使用FFTW組件.Windows下的的FFTW組件已經編譯好成爲dll文件,按照開發應用的 ...
- 『Python基础-8』列表
『Python基础-8』列表 1. 列表的基本概念 列表让你能够在一个地方存储成组的信息,其中可以只包含几个 元素,也可以包含数百万个元素. 列表由一系列按特定顺序排列的元素组成.你可以创建包含字母表 ...
- 从零开始一个http服务器(四)-动态返回
从零开始一个http服务器(四) 代码地址 : https://github.com/flamedancer/cserver git checkout step4 运行: make clean &am ...
- go学习笔记-反射(Reflection)
反射(Reflection) 反射是利用reflect包实现的 反射可大大提高程序的灵活性,使得interface{}有更大的发挥余地 反射使用TypeOf和ValueOf函数从接口中获取目标对象信息 ...
- Hibernate学习笔记三
1.1.1 Hibernate的关联关系映射:(多对多) 1.1.1.1 多对多的配置: 步骤一创建实体和映射: Student: public class Student { private Int ...
- 【blockly教程】第五章 循环结构
在这里,我们将介绍一个新游戏--Pond Tutor 在Pond Tutor(https://blockly-games.appspot.com/pond-tutor)这个游戏中,我们将扮演黄色的鸭子 ...