POJ 2987 Firing (网路流之最大权闭合图)

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.

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条边,u,v链接表示v是u的下属,选择一个点必须选择他的下属。问你选择点的最大权值和是多少,选择的点个数是多少。

思路:真是巧妙的做法,这是一种最大权闭合图。所以做法如下:
来源博客:http://www.hankcs.com/program/algorithm/poj-2987-firing.html
这里写图片描述

不过这里不是很容易想明白,我说一下我自己的想法,如果把正权值之和比作盈利之和,负权边表示经过该点的必要花费。那么该图的最小割就是想盈利这么多的最小花费。所以正权值之和-最小割就是盈利最大的。也就是最大权。而对于残余网络中能到达的点就是裁员的点。我的思考是,对于正权的满流边相当于是这条边的钱全被花完,那么我们一定不选这个点。也不会选择从这个点的增广路径。而对于未漫流的正权点,我们一定是选他的增广路径。因为从这里走是盈利的。所以残余网络中能到达的点,就是要裁员的点。(这里的想法是自己臆想出来的)

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int MAX = 5010;
typedef long long ll;
const ll INF = 0x7ffffffffffff;
class Edge{
public:
    int to,rev;
    ll cap;
    Edge();
    Edge(int _to,ll _cap,int _rev);
};
int N,M;
vector<Edge> G[MAX];
int level[MAX],iter[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,0,G[from].size()-1));
}
void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    que.push(s);
    level[s] = 0;
    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 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 > 0 && level[v] < level[e.to]){
            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;
}
ll Max_Flow(int s,int t){
    ll flow = 0;
    while(true){
        bfs(s);
        if(level[t] < 0)    return flow;
        memset(iter,0,sizeof(iter));
        ll f;
        while((f = dfs(s,t,INF)) > 0){
              flow += f;
        }
    }
}
bool used[MAX];
void GetNum(int v,int &num){
    num++;
    used[v] = true;
    for(int i=0;i<G[v].size();++i){
        Edge &e = G[v][i];
        if(e.cap > 0 && !used[e.to]){
            GetNum(e.to,num);
        }
    }
}
//#define local
int main(void){
    #ifdef local
        freopen("in.txt","r",stdin);
    #endif
    scanf("%d%d",&N,&M);
    ll w,Rsum = 0;
    for(int i=1;i<=N;++i){
        scanf("%lld",&w);
        if(w > 0){
            Rsum += w;
            add_Edge(0,i,w);
        }
        else{
            add_Edge(i,N+1,-w);
        }
    }
    int u,v;
    for(int i=1;i<=M;++i){
        scanf("%d%d",&u,&v);
        add_Edge(u,v,INF);
    }
    ll res = Max_Flow(0,N+1);
    int num = 0;
    memset(used,false,sizeof(used));
    GetNum(0,num);
    printf("%d %lld\n",num-1,Rsum-res);
    return 0;
}

Edge::Edge(){
    to = cap = rev = 0;
}
Edge::Edge(int _to,ll _cap,int _rev){
    to = _to;
    cap = _cap;
    rev = _rev;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值