poj 2987 Firing(最大权闭合图)

Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 6198   Accepted: 1875

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

POJ Monthly--2006.08.27, frkstyc

题目:http://poj.org/problem?id=2987

题意:给你n个员工,和员工之间的关系图,解雇一每个员工可能带来收益,也可能带来损失,并且,解雇掉一个员工,就必须解雇他所有直接或间接的下属,现在求一种解雇方案,使收益最大,在这个前提下,使得解雇的人最少。。。

分析:如果只需要收益最大,那么这就是一个最大权闭合图的问题,可以转换为最小割的问题,进而用最大流的算法来做,具体的在胡伯涛大神的论文里有讲,为了做这题,我特意认真的去看了下,发现以前好多都没弄明白。。。

具体的转换就是创建一个源点和一个汇点,一个员工的收益大于0的就在源点与它之间连一条容量为收益值的边,小于零则和汇点连,容量为损失的值,然后关系边保留,并且容量都为无穷。。。

对于第二问,如果没有真正明白这个模型的话,估计就无从下手了。其实只要从源点出发,遍历残余网络就行,标记所有遍历到的点,这些点就是要选择的点。。。

其实我讲不明白的,就给组数据吧= =


3 2
4
3
-6
1 3
2 3

代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int oo=1e9;
const int mm=222222;
const int mn=5555;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],vis[mn];
void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1,vis[i]=0;
    edge=0;
}
void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
long long Dinic_flow()
{
    int i,delta;
    long long ret=0;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
void dfs(int u)
{
    vis[u]=1;
    for(int i=head[u],v;i>=0;i=next[i])
        if(flow[i]&&!vis[v=ver[i]])dfs(v);
}
int main()
{
    int i,j,k,n,m;
    long long sum,ans;
    while(~scanf("%d%d",&n,&m))
    {
        prepare(n+2,0,n+1);
        sum=0;
        for(i=1;i<=n;++i)
        {
            scanf("%d",&k);
            if(k<0)addedge(i,dest,-k);
            if(k>0)addedge(src,i,k),sum+=k;
        }
        while(m--)
        {
            scanf("%d%d",&i,&j);
            addedge(i,j,oo);
        }
        ans=sum-Dinic_flow();
        dfs(0);
        sum=0;
        for(i=1;i<=n;++i)sum+=vis[i];
        printf("%I64d %I64d\n",sum,ans);
    }
    return 0;
}


你可能感兴趣的:(算法,OO,input,each,output,firing)