• poj 2987 Firing


    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 = 5000+4, M_MAX = 200000, V_MAX = N_MAX + 2;
    
    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, 0, G[from].size() - 1));
    }
    
    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 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;
        for (;;) {
            bfs(s);
            if (level[t] < 0)return flow;
            memset(iter, 0, sizeof(iter));
            ll f;
            while ((f = dfs(s, t, INT_MAX))>0) {
                flow += f;
            }
        }
    }
    
    int res = 0;
    bool vis[V_MAX];
    void solve(int v) {//深搜找点
        res++;
        vis[v] = true;
        for (int i = 0; i < G[v].size();i++) {
            edge e = G[v][i];
            if (e.cap > 0 && !vis[e.to]) {
                solve(e.to);
            }
        }
    }
    
    
    int main() {
        while (scanf("%d%d",&n,&m)!=EOF) {
            res = 0; memset(vis, 0, sizeof(vis));
            int s = 0, t = n+1,V=t+1;
            ll sum = 0;
            for (int i = 0; i < n;i++) {
                ll a;
                scanf("%lld",&a);
                if (a > 0) { add_edge(s, i + 1, a); sum += a; }
                else add_edge(i + 1, t, -a);
           }
            for (int i = 0; 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
    ",--res,sum-flow);
            for (int i = 0; i < V;i++) {//向量清空
                G[i].clear();
            }
        }
        return 0;
    }
  • 相关阅读:
    jdk动态代理
    HTML+JavaScript实现在一个下拉框中多选,然后提交到另外一个下拉框中的效果
    Top中是如何取到Linux内核中的Hertz的?以及CPU使用率到底是怎么算出来的?
    C语言中的负数是如何表示的?
    Learning Python第二版笔记-Chapter 3 How to run your program
    Learning Python第二版笔记-Chapter 4 Numbers
    Learning Python第二版笔记-Chapter 1 & 2
    Firefox中的document.all的替代方案From EasyCluster support Firefox
    Linux下共享库中的全局变量,静态变量是否只有一份?
    C和C++编程中static关键字的含义-修饰函数和变量
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/7207661.html
Copyright © 2020-2023  润新知