Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 10696   Accepted: 3226

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.

Sample Input

5 5
8
-9
-20
12
-10
1 2
2 5
1 4
3 4
4 5

Sample Output

2 2

Hint

As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.
 
题意:公司大裁员,希望尽可能的少裁剪员工,且裁掉以后所能获得的总利润最大化,输出裁剪的人数和获得的最大利润。裁员的时候裁掉某一个人,那么连同这个人的所有下属都将会被裁掉。

最大权闭合图的求解方法是

  1. 先构造网络流N,添加源点s,从s到正权值点做一条边,容量为点的权值。

  2. 添加汇点t,从负权值点到t做一条边,容量为点的权值的绝对值。

  3. 原来的边的容量统统设为无穷大。

  4. 求解最小割,最大权=正权值之和-最小割权值

  5. 残余网络中的点的个数即为裁员个数。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N_MAX = +, M_MAX = , V_MAX = N_MAX + ; typedef long long ll;
int n, m;
struct edge {
int to; ll cap;int rev;
edge(int to, ll cap, int rev) :to(to), cap(cap), rev(rev) {}
};
vector<edge>G[V_MAX];
int level[V_MAX];
int iter[V_MAX]; void add_edge(int from, int to, ll cap) {
G[from].push_back(edge(to, cap, G[to].size()));
G[to].push_back(edge(from, , G[from].size() - ));
} void bfs(int s) {
memset(level, -, sizeof(level));
queue<int>que;
level[s] = ;
que.push(s);
while (!que.empty()) {
int v = que.front(); que.pop();
for (int i = ; i < G[v].size(); i++) {
edge&e = G[v][i];
if (e.cap > && level[e.to] < ) {
level[e.to] = level[v] + ;
que.push(e.to);
}
}
}
} ll dfs(int v, int t, ll f) {
if (v == t)return f;
for (int &i = iter[v]; i < G[v].size(); i++) {
edge&e = G[v][i];
if (e.cap > && level[v] < level[e.to]) {
ll d = dfs(e.to, t, min(f, e.cap));
if (d > ) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
} ll max_flow(int s, int t) {
ll flow = ;
for (;;) {
bfs(s);
if (level[t] < )return flow;
memset(iter, , sizeof(iter));
ll f;
while ((f = dfs(s, t, INT_MAX))>) {
flow += f;
}
}
} int res = ;
bool vis[V_MAX];
void solve(int v) {//深搜找点
res++;
vis[v] = true;
for (int i = ; i < G[v].size();i++) {
edge e = G[v][i];
if (e.cap > && !vis[e.to]) {
solve(e.to);
}
}
} int main() {
while (scanf("%d%d",&n,&m)!=EOF) {
res = ; memset(vis, , sizeof(vis));
int s = , t = n+,V=t+;
ll sum = ;
for (int i = ; i < n;i++) {
ll a;
scanf("%lld",&a);
if (a > ) { add_edge(s, i + , a); sum += a; }
else add_edge(i + , t, -a);
}
for (int i = ; i < m;i++) {
int a, b;
scanf("%d%d",&a,&b);
add_edge(a,b,INF);
}
ll flow=max_flow(s, t);
solve(s);
printf("%d %lld\n",--res,sum-flow);
for (int i = ; i < V;i++) {//向量清空
G[i].clear();
}
}
return ;
}

poj 2987 Firing的更多相关文章

  1. poj 2987 Firing 最大权闭合图

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

  2. POJ 2987 - Firing - [最大权闭合子图]

    题目链接:http://poj.org/problem?id=2987 Time Limit: 5000MS Memory Limit: 131072K Description You’ve fina ...

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

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

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

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

  5. POJ 2987 Firing 最大流 网络流 dinic 模板

    https://www.cnblogs.com/137shoebills/p/9100790.html http://poj.org/problem?id=2987 之前写过这道题,码一个dinic的 ...

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

    Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12108   Accepted: 3666 Descript ...

  7. POJ 2987 Firing(最大流最小割の最大权闭合图)

    Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...

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

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

  9. POJ 2987 Firing | 最大权闭合团

    一个点带权的图,有一些指向关系,删掉一个点他指向的点也不能留下,问子图最大权值 题解: 这是最大权闭合团问题 闭合团:集合内所有点出边指向的点都在集合内 构图方法 1.S到权值为正的点,容量为权值 2 ...

随机推荐

  1. 关于父类中的this指针的问题

    在处理一个消息推送的问题的时候遇到个小问题,比如A是B的子类,当A生成实例时,会执行父类的构造函数,那么在父类中,this会是什么类型呢? 于是做了个小测试 子类ChildClass: public ...

  2. PHP开发基础视频教程

    PHP现今作为互联网运用很广泛的编程语言,市场需求量也越来越高,而PHP开发工程师的薪资也是一路水涨船高,更多的人看到了PHP的发展前景,纷纷都想投入到PHP的开发大军中来,那么对于很多转行或者零基础 ...

  3. Lesson2

    #ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #include <SDL/ ...

  4. UVA 247 - Calling Circles (Floyd)

    互相可以打电话是一个传递关系,所以Floyd求传递封包,dfs找一个尽量大的圈. #include<bits/stdc++.h> using namespace std; ; map< ...

  5. shell 简单脚本 2

    #!/bin/bash source /etc/profile APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars" APPLICAT ...

  6. @ConditionalOnProperty来控制Configuration是否生效

    1. 简介 Spring Boot通过@ConditionalOnProperty来控制Configuration是否生效 2. 说明 @Retention(RetentionPolicy.RUNTI ...

  7. Makefile入门教程

    Makefile介绍 make是一个命令工具,它解释Makefile 中的指令(应该说是规则).在Makefile文件中描述了整个工程所有文件的编译顺序.编译规则.Makefile 有自己的书写格式. ...

  8. html输入框去除记忆功能

    自动完成功能,只需把AUTOCOMPLETE设为off即可,如: 整个表单禁止自动完成 HTML code <FORM method=post action="submit.asp&q ...

  9. python之set (集合)

    1. 集合是什么 set {1,2,3} 2. 集合怎么用 去重 集合是无序的 集合就是一个没有值的字典,遵循:唯一,无序,元素要求可哈希(不可变) 集合是可变的 2.1 增 方法一: s.updat ...

  10. QT5:先导篇 全局定义

    一.简介 <QtGlobal>头文件包含了Qt类库的一些全局定义,包含基本数据类型 函数和宏 二.全局变量定义 <QtGlobal>定义的数据类型: Qt数据类型        ...