POJ 2987 Firing 最大权闭合图


Firing
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 7976 Accepted: 2409

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


一个公司要炒掉一些人,每个人都有一个权值,如果权值为正,则公司获利,否则公司赔本。另外,炒掉一个人之后,这个人的下级也要被炒掉,问如何才能获得最大利润。
求最大利润可以转换为最大权闭合图。
第一问求至少要裁掉多少人。因为求完最小割之后,残留网络图中与源点相连的且权值不为0的点就是要裁掉的,然后dfs这些点,因为他们的下级也要被裁掉,直至权值为0.
//4112K	407MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const __int64 MAXN=20010;//点数的最大值
const __int64 MAXM=880010;//边数的最大值
const __int64 INF=0x3f3f3f3f;

struct Node
{
    __int64 from,to,next;
    __int64 cap;
} edge[MAXM];
__int64 tol;
__int64 head[MAXN];
__int64 dis[MAXN];
__int64 gap[MAXN];//gap[x]=y :说明残留网络中dis[i]==x的个数为y
__int64 vis[MAXN];
__int64 nn;//nn是总的点的个数,包括源点和汇点

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}

void addedge(__int64 u,__int64 v,__int64 w)
{
    edge[tol].from=u;
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].from=v;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}
void BFS(__int64 start,__int64 end)
{
    memset(dis,-1,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=1;
    __int64 que[MAXN];
    __int64 front,rear;
    front=rear=0;
    dis[end]=0;
    que[rear++]=end;
    while(front!=rear)
    {
        __int64 u=que[front++];
        if(front==MAXN)front=0;
        for(__int64 i=head[u]; i!=-1; i=edge[i].next)
        {
            __int64 v=edge[i].to;
            if(dis[v]!=-1)continue;
            que[rear++]=v;
            if(rear==MAXN)rear=0;
            dis[v]=dis[u]+1;
            ++gap[dis[v]];
        }
    }
}
__int64 SAP(__int64 start,__int64 end)
{
    __int64 res=0;
    nn=end+1;
    BFS(start,end);
    __int64 cur[MAXN];
    __int64 S[MAXN];
    __int64 top=0;
    memcpy(cur,head,sizeof(head));
    __int64 u=start;
    __int64 i;
    while(dis[start]<nn)
    {
        if(u==end)
        {
            __int64 temp=INF;
            __int64 inser;
            for(i=0; i<top; i++)
                if(temp>edge[S[i]].cap)
                {
                    temp=edge[S[i]].cap;
                    inser=i;
                }
            for(i=0; i<top; i++)
            {
                edge[S[i]].cap-=temp;
                edge[S[i]^1].cap+=temp;
            }
            res+=temp;
            top=inser;
            u=edge[S[top]].from;
        }
        if(u!=end&&gap[dis[u]-1]==0)//出现断层,无增广路
            break;
        for(i=cur[u]; i!=-1; i=edge[i].next)
            if(edge[i].cap!=0&&dis[u]==dis[edge[i].to]+1)
                break;
        if(i!=-1)
        {
            cur[u]=i;
            S[top++]=i;
            u=edge[i].to;
        }
        else
        {
            __int64 min=nn;
            for(i=head[u]; i!=-1; i=edge[i].next)
            {
                if(edge[i].cap==0)continue;
                if(min>dis[edge[i].to])
                {
                    min=dis[edge[i].to];
                    cur[u]=i;
                }
            }
            --gap[dis[u]];
            dis[u]=min+1;
            ++gap[dis[u]];
            if(u!=start)u=edge[S[--top]].from;
        }
    }
    return res;
}
void dfs(__int64 x)
{
    vis[x]=1;
    for(__int64 u=head[x];u!=-1;u=edge[u].next)
    {
        __int64 v=edge[u].to;
        if(!vis[v]&&edge[u].cap>0)
            dfs(v);
    }
}
int main()
{
    __int64 s,t,n,m;
    scanf("%I64d%I64d",&n,&m);
    memset(head,-1,sizeof(head));
    init();
    s=0;
    t=n+1;
    __int64 a,b,count=0;
    __int64 sum=0;
    for(__int64 i=1; i<=n; i++)
    {
        scanf("%I64d",&a);
        if(a>0){addedge(s,i,a);sum+=a;}
        if(a<0)addedge(i,t,-a);
    }
    for(__int64 i=1; i<=m; i++)
    {
        scanf("%I64d%I64d",&a,&b);
        addedge(a,b,INF);
    }
    __int64 ans=SAP(s,t);
    dfs(0);
    for(__int64 i=1;i<=n;i++)
        if(vis[i])count++;
    printf("%I64d %I64d\n",count,sum-ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值