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. spring 常用问题汇总

    2018.02.06 SpringMVC中StringHttpMessageConverter乱码处理 http://blog.csdn.net/wangyangbto/article/details ...

  2. Xcode编译工具

    一.关于Other Linker Flags xcode中,在“Targets”选项下有Other Linker Flags选项,在这里可以填写xcode链接器的参数,如:-ObjC.-all_loa ...

  3. Maven归纳

      一.常用功能 1.Maven的中央仓库 https://mvnrepository.com/ 2.添加jar包依赖 1.首先点击pom.xml,然后点击弹出页面中的Dependencies选项,接 ...

  4. OpenRead方法打开文件并读取

    实现效果: 知识运用: File类的OpenRead方法 //实现打开现有文件以进行读取 public static FileStream OpenRead(string path) FileStre ...

  5. C基础:关于预处理宏定义命令

    为了程序的通用性,可以使用#define预处理宏定义命令,它的具体作用,就是方便程序段的定义和修改. 1.关于预定义替代 #define Conn(x,y) x##y#define ToChar(x) ...

  6. java运行环境jdk的安装和环境变量的配置教程

    jdk的下载与安装 一.官网下载jdk 1.百度搜索jdk,进入官网,如下图所示: 官网下载jdk图1 2.在官网网站中找到合适的版本下载(以最新版本为例),如下图所示: 官网下载jdk图2 官网下载 ...

  7. insert size|single-read|Paired-end|Mate-pair

    (测序方面):测三只大熊猫:得到的insert size有150bp,500bp,2kb,5kb和10kb这四种,可测得序列长度和平均reads长度. 为什么average reads这么短? 因为i ...

  8. POI导出excel项目(webwork)实例

    后台action: public String exportExcel(){ this.setUserList(this.getUserService().findUserInfosByGroupID ...

  9. (转发)IOS高级开发~Runtime(一)

    IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...

  10. ios 自定义RadioButton

    1 前言 众所周知在IOS中没有单选按钮这一控件,今天我们来学习一下简单的单选控件.类似与Web中的radio表单元素. 2 详述 本控件单纯的利用按钮控件和NSObject的respondsToSe ...