Firing POJ - 2987(最大权闭合图)

Firing POJ - 2987

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?InputThe 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.OutputOutput 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.
题意:有个公司,n个员工,m个关系,因为亏损,所以要辞退一些员工,给出辞退每个员工会给带来的收益(有正有负),关系x y代表x是y的上司,如果辞退一个上司,那么他手下的人都会退出,问最大的收益,和要删除的人数。

因为删掉一个上司,员工也会离开,所以最后求的删除的人会是一个闭合图,也就是求最大权闭合图

最大权闭合图的求法:
①先记录整个图中,所有正点权值的和;
②建立对应流网络,求最大流,最大流在数值上等于最小割,故我们得到了流网络的s-t最小割;
③“所有正点权值的和”减去“s-t最小割”,即得最大权闭合子图的权值和。

解释:
https://www.cnblogs.com/dilthey/p/7565206.html

#include <iostream>
#include <vector>
#include <queue>
#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
struct edge{
    int to, rev;
    ll cap;
    edge(int to, ll cap, int rev) :to(to), cap(cap), rev(rev){}
};
const int MAX_V = 5e3 + 10;
vector<edge> G[MAX_V];
int used[MAX_V];
int level[MAX_V];
const int INF = 0x3f3f3f3f3f3f;


void add_edge(int from, int to, int cap) {
	G[from].push_back(edge(to, cap, G[to].size()));
	G[to].push_back(edge(from, 0, G[from].size() - 1));
}

ll dfs(int v, int t, ll f) {
    if(v == t) return f;
    for(int i = used[v]; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if(level[v] < level[e.to] && e.cap > 0) {
            ll d = dfs(e.to, t, min(f, e.cap));
            if(d > 0) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty()) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < G[v].size(); i++) {
			edge& e = G[v][i];
			if (e.cap > 0 && level[e.to] < 0) {
				level[e.to] = level[v] + 1;
				que.push(e.to);
			}
		}
    }
}

ll max_flow(int s, int t) {
    ll flow = 0;
    for(;;) {
        bfs(s);
        if(level[t] < 0) return flow;
        memset(used, 0, sizeof(used));
        ll f;
        while((f = dfs(s, t, INF)) > 0) // 千万别写成 f = dfs(s, t, INF) > 0,这是赋值
            flow += f;
    }
}

int cnt = 0;
bool visited[MAX_V];

void solve(int s) {
    cnt++;
    visited[s] = 1;
    for (int i = 0; i < int(G[s].size()); i++) {
        edge& e = G[s][i];
        if (e.cap > 0 && !visited[e.to]) {
            solve(e.to);
        }
    }
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    ll sum = 0;
    int s = 0, t = n + 1;
    //加超级源点s,超级汇点t
    for(int i = 1; i <= n; i++) {
        int w;
        scanf("%d", &w);
        if(w > 0) {
            sum += w;
            add_edge(s, i, w);
        }
        else if(w < 0){
            add_edge(i, t, -w);
        }
    }
    for(int i = 1; i <= m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b, INF);
    }
    ll ans = sum - max_flow(s, t);
    solve(s);
    printf("%d %lld\n", --cnt, ans);
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值