【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.

分析: 很明显是个最大权闭合图的问题,但是关键就是 个数是多少呢?最小的个数怎么求呢?
首先:怎么找到最大权闭合图中 点的个数呢? 想一下,我们求解的过程利用的是最小割(割边一定是满流),而且最小割划分的两个集合S,T, 源点s和最大权闭合图集合 是相连的,共同属于S集合。所以,我们可以从s出发 ,遍历所有未满流的边,途中访问到的点就是 S集合。
个数找到了,但是最小的呢?
结论– 最大权闭合图 的势(点的个数) 一定是 唯一的。

不想看证明的忽视下方:(比如我,逃 ….
Orz
命题: 根据闭合图定义建图G,在对图G运行最大流后,最大权闭合图V的势是唯一的.
证明:设两个最大全闭合图V1,V2,它们的权值相等,不失一般性,假设|V1|<|V2|.考虑以下情况:
1)若V1包含于V2,因为|V1|<|V2|,则V2-V1不等于空集.同时,不存在从V1到V2-V1的边,否则根闭合图定义矛盾.由于V1和V2的权值相同,则说明V2-V1的权值为0.根据最大权闭合图的求法以及运行过最大流可知,源点S不可能到达V2-V1.否则与V2-V1权值为0矛盾.进而,运行最大流以后,实际上只有|V1|大小的最大权闭合图.
2)若V1∩V2等于空集,则把V1和V2合并将产生一个更大的闭合图.因此,V1∩V2不等于空集,不妨记为S=V1∩V2,则不存在从S到V1-S或V2-S的边,若存在x属于V1-S,则对于V2来说,与闭合图定义矛盾,同理知V1.根据已知有,W(S)+W(V1-S)=W(S)+W(V2-S),即W(V1-S)=W(V2-S),若W(V1-S)>0,则将V1,V2合并会产生更大的闭合图,矛盾;若W(V1-S)<0,则将V1-S和V2-S去掉,使得S本身为一个闭合图,且它的权比V1或V2大,矛盾.因此W(V1-S)=W(V2-S)=0,由1)的证明知,从源点不可达V1-S或V2-S,因此,实际上得到的最大权闭合图就是S,其权值与V1,V2相同.
综上所述,运行最大流后,最大权闭合图的势是唯一的.

代码

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
#define LL long long

const int N = (int)5000+11;
const int M = (int)60000*3+11;
const int mod = 1e9+7;
const int inf = (int)0x3f3f3f3f;

struct Edge {
    int form,to,cap,flow,nexts;
}edge[M];
int head[N],top;
void init(){
    memset(head,-1,sizeof(head));
    top=0;
}
void addedge(int a,int b,int c){
    Edge e={a,b,c,0,head[a]};
    edge[top]=e;head[a]=top++;

    Edge ee={b,a,0,0,head[b]};
    edge[top]=ee;head[b]=top++;
}
int vis[N],dis[N];
int cur[N];
bool bfs(int st,int ed){
    queue<int>Q;
    memset(vis,0,sizeof(vis));
    memset(dis,-1,sizeof(dis));
    Q.push(st);vis[st]=1;dis[st]=1;
    while(!Q.empty()){
        int now=Q.front();Q.pop();
        for(int i=head[now];i!=-1;i=edge[i].nexts){
            Edge e=edge[i];
            if(!vis[e.to]&&e.cap-e.flow>0){
                vis[e.to]=1;
                dis[e.to]=dis[now]+1;
                if(e.to==ed) return 1;
                Q.push(e.to);
            }
        }
    }
    return 0;
}
LL dfs(int now,LL a,int ed){
    if(a==0||now==ed) return a;
    LL flow=0,f;
    for(int &i=cur[now];i!=-1;i=edge[i].nexts){
        Edge &e=edge[i];
        if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(e.cap*1ll-e.flow,a),ed))>0){
            e.flow+=f;
            flow+=f;
            edge[i^1].flow-=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
LL max_flow(int st ,int ed){
    LL flow=0;
    while(bfs(st,ed)){
        memcpy(cur,head,sizeof(head));
        flow+=dfs(st,inf,ed);
    }
    return flow;
}
int cnt;
void Find(int now){
    cnt++; vis[now]=1;
    for(int i=head[now];i!=-1;i=edge[i].nexts){
        Edge e=edge[i];
        if( !vis[e.to]  && e.cap!=e.flow) Find(e.to);
    }
}

int main( ){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        int S=0,T=n+1;  LL ans=0; init();
        for(int i=1;i<=n;i++){
            int c; scanf("%d",&c);
            if(c>0 ) addedge(S,i,c),ans+=c;
            else addedge(i,T,-c);
        }
        while(m--){
            int a ,b; scanf("%d%d",&a,&b);
            addedge(a,b,inf);
        }
        ans-=max_flow(S,T);
        //cout<<"=="<<endl;
        memset(vis,0,sizeof(vis));
        cnt=0; Find(S);
        printf("%d %lld\n",--cnt,ans);
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值