「模板」(多路增广 + 当前弧优化) POJ 2987: Firing(最大权闭合图)

  • 题目链接:http://poj.org/problem?id=2987
  • 题意:一个公司要裁员,裁员可以给公司省下其工资及奖金。开除一个员工时,要把他的下属也开除。一个员工可以属于多个部门,在一个部门中,一个员工的下属可能在另一个部门是他的上司。
  • 补充知识点
    • 闭合图:对于一个有向图G,存在点集合V,任取点u属于V,u的出边的另一个点也属于V,则为闭合图。
    • 理解:任取一起点,终点必定是无出度的点。
    • 最大权闭合子图:当每个点有一个权值w(有正有负),点权和最大的闭合图为最大权闭合子图。
    • 如下图,最大权闭合子图为点集{3,4,5},最大权为7+0-3=4。
      在这里插入图片描述
  • 思路:做最大权闭合图的套路:把权值为正的点与超级源点S相连,容量为该权值,把权值为负的点与超级汇点T相连,容量为该权值的绝对值,然后点与点之间的连边是,如果a被删除时b也要被删除,那么a连一条边指向b,容量为INF。如果要求删除的点数的话,从S开始DFS,残余网络中的点即删除的点。因为删除的点的集合是从S出发可以达到的并且不能达到T的点集(即割分成的两个集(S集和T集)的S集)。
    复制代码
  • 注意:设一个员工a有一个下属b,则建图时为a指向b,这样才能建出符合题意的闭合图。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#define pi acos(-1)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 50000 + 10;
const int maxv = 5000 + 10;
const int maxe = 60010 * 2;
const int mod = 1e9 + 7;

int b[maxv];


//

int s, t;
int head[maxe], cnt=0, nowedge[maxe];
int dis[maxv];
int vis[maxv];
int n, f, d;

struct Edge
{
    int to, next, cap;
}es[maxe];

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

void add(int u, int v, int vol)
{
    es[cnt].to = v;
    es[cnt].cap = vol;
    es[cnt].next = head[u];
    head[u] = cnt++;

    es[cnt].to = u;
    es[cnt].cap = 0;
    es[cnt].next = head[v];
    head[v] = cnt++;
}

bool BFS()
{
    queue<int> que;
    memset(dis, -1, sizeof(dis));
    dis[s] = 0;
    que.push(s);
    int cur, v;
    while(!que.empty())
    {
        cur = que.front(); que.pop();
        for(int i=head[cur]; i!=-1; i=es[i].next){
            v = es[i].to;
            if(dis[v]==-1 && es[i].cap>0){
                dis[v] = dis[cur]+1;
                que.push(v);
            }
        }
    }
    if(dis[t] == -1) return false;
    else return true;
}

LL DFS(int cur, LL low)
{
    LL res=0, add=0;// 多路增广
    if(cur == t) return low;
    for(int i=nowedge[cur]; i!=-1; i=es[i].next){
        nowedge[cur] = i;
        if(dis[es[i].to]==dis[cur]+1
        && es[i].cap>0
        && (add = DFS(es[i].to, min(low, (LL)es[i].cap))) ){
            es[i].cap -= add;
            es[i^1].cap += add;
            low -= add;
            res += add;
            if(low == 0) break;
        }
    }
    if(res == 0) dis[cur] = 0;
    return res;
}

LL MAXFLOW()
{
    LL ans=0 , tmp=0;
    while(BFS()){
        for(int i=s; i<=t; i++){
            nowedge[i] = head[i];
        }
        if(tmp = DFS(s, INF))
            ans += tmp;
    }
    return ans;
}


int left_net(int cur)
{
    vis[cur] = 1;
    int ans = 1;
    for(int i=head[cur]; i!=-1; i = es[i].next){
        if(es[i].cap > 0 && !vis[es[i].to]) ans += left_net(es[i].to);
    }
    return ans;
}

int main()
{

    int n, m;
    while(~scanf("%d%d", &n, &m)){
        init();
        s = 0;
        t = n+1;

        LL sum = 0;
        for(int i=1; i<=n; i++){
            scanf("%d", &b[i]);
            if(b[i] > 0) add(s, i, b[i]), sum+=b[i];
            else add(i, t, -b[i]);
        }
        for(int i=1; i<=m; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v, INF);
        }
        LL ans = sum - MAXFLOW();
        printf("%d %lld\n", left_net(s)-1, ans);
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值