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 ≤ ij ≤ 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(最大流最小割の最大权闭合图)的更多相关文章

  1. 【BZOJ-3438】小M的作物 最小割 + 最大权闭合图

    3438: 小M的作物 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 825  Solved: 368[Submit][Status][Discuss ...

  2. 【POJ 2987】Firing (最小割-最大权闭合子图)

    裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...

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

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

  4. poj 2987 Firing 最大权闭合图

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

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

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

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

    [题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...

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

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

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

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

  9. poj 2987 最大权闭合图

    Language: Default Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 8744   Accept ...

随机推荐

  1. Excel2013打开提示 文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?

    有的时候打开xls文档时,会提示“文件格式和扩展名不匹配.文件可能已损坏或不安全.除非您信任其来源,否则请勿打开.是否仍要打开它?” 遇到这种情况,我们需要 1.win键+R键,打开“运行“,输入re ...

  2. 阿里云Docker镜像仓库(Docker Registry)

    镜像仓库申请地址: https://cr.console.aliyun.com/cn-shanghai/instances/repositories   一.创建命名空间 例如daniel-hub   ...

  3. SQL Server 2012 - 多表连接查询

    -- 交叉连接产生笛卡尔值 (X*Y) SELECT * FROM Student cross Join dbo.ClassInfo --另外一种写法 SELECT * FROM Student , ...

  4. Python入门 —— 06语音识别

    Python 语音 实现语音操控的原理 语音操控分为语音识别和语音朗读两部分 我们使用speech模块实现语音模块(python 2.7) SAPI是微软Speech API , 是微软公司推出的语音 ...

  5. 详解HTML5中的进度条progress元素简介及兼容性处理

    一.progress元素基本了解 1.基本知识 progress元素属于HTML5家族,指进度条.IE10+以及其他靠谱浏览器都支持. 注释:Internet Explorer 9 以及更早的版本不支 ...

  6. VUE通过索引值获取数据不渲染的问题

    问题:vue里面当通过索引值获取数据时,ajax数据成功返回,但是在火狐下不渲染 解决:

  7. U盘kali系统安装

      正言: 起初先百度了一下U盘安装Kali的资料,有很多版本和方法,当然还是以百度经验为例开始操作https://jingyan.baidu.com/article/cdddd41ca1027e53 ...

  8. 指针小白:修改*p与p会对相应的地址的变量产生什么影响?各个变量指针的长度为多少?

    这两天敲代码碰到了一个这样的问题 代码如下: #include <stdio.h> #include <stdlib.h> int main() { ; int* p=& ...

  9. linux进程篇 (三) 进程间的通信2 信号通信

    2. 信号通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...

  10. sigmoid function和softmax function

    sigmoid函数(也叫逻辑斯谛函数):  引用wiki百科的定义: A logistic function or logistic curve is a common “S” shape (sigm ...