poj2987 Firing

Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 11132   Accepted: 3372

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.

Source

题目大意:每个点的点权有正有负,删掉一个点需要删掉它的所有后继节点以及后继节点的后继节点直到无点可删.求最大收益时删的点数.
分析:最大权闭合图.
          固定的建图方法:1.保留原图中的所有边,边权为inf. 2.对于点权为正的点,从源点S向它连一条容量为其点权的边.  3.对于点权为负的点,从它向汇点T连一条容量为它的点权的绝对值的边.  4.点权为正的点的点权和 - 最大流即为答案.
          至于会删掉多少个点,最大权闭合图的所有点都会被删掉,那么dfs求一下还有多少个点在残余网络上就可以了.
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll maxn = 100010,inf = 0x7fffffff;

ll n,f,d,S,T,head[maxn],to[maxn],nextt[maxn],tot = 2,w[maxn],ans,m,c[maxn],sum,flag[maxn];
ll vis[maxn],ans2;

void add(ll x,ll y,ll z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;

    w[tot] = 0;
    to[tot] = x;
    nextt[tot] = head[y];
    head[y] = tot++;
}

bool bfs()
{
    queue <ll> q;
    memset(vis,-1,sizeof(vis));
    vis[S] = 0;
    q.push(S);
    while (!q.empty())
    {
        ll u = q.front();
        q.pop();
        if (u == T)
            return true;
        for (int i = head[u];i;i = nextt[i])
        {
            ll v = to[i];
            if (vis[v] == -1 && w[i])
            {
                vis[v] = vis[u] + 1;
                q.push(v);
            }
        }
    }
    return false;
}

ll dfs(ll u,ll f)
{
    ll res = 0;
    if (u == T)
        return f;
    for (int i = head[u];i;i = nextt[i])
    {
        ll v = to[i];
        if (w[i] && vis[v] == vis[u] + 1)
        {
            ll temp = dfs(v,min(f - res,w[i]));
            res += temp;
            w[i] -= temp;
            w[i ^ 1] += temp;
            if (res == f)
                return res;
        }
    }
    if (!res)
        vis[u] = -1;
    return res;
}

void dfs2(ll u)
{
    flag[u] = 1;
    ans2++;
    for (int i = head[u];i;i = nextt[i])
    {
        ll v = to[i];
        if (w[i] && !flag[v])
        {
            flag[v] = 1;
            dfs2(v);
        }
    }
}

void dinic()
{
    while (bfs())
       ans += dfs(S,inf);
}

int main()
{
    scanf("%lld%lld",&n,&m);
    S = 0;
    T = n + 1;
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld",&c[i]);
        if (c[i] > 0)
        {
            sum += c[i];
            add(S,i,c[i]);
        }
        if (c[i] < 0)
            add(i,T,-c[i]);
    }
    for (int i = 1; i <= m; i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b,inf);
    }
    dinic();
    dfs2(S);
    printf("%lld %lld\n",ans2 - 1,sum - ans);

    return 0;
}

 

posted @ 2017-12-29 00:01  zbtrs  阅读(188)  评论(0编辑  收藏  举报