POJ 2987 Firing 最小割(最大权闭合子图)

最大权闭合子图。。。闭合子图满足在子图内的任意点在原图中存在出边总是指向该闭合子图。
如果添加了超级源汇,那么这个图由一个割被分成了两个集合[S,T],且T是闭合子图。
最小割!
让我们来推导一下最大权闭合子图的解决方法。
xi 表示点i是否在闭合子图中,0在,1不在。
那么答案很显然了:

max{max{0,1xi}wi<i,j>Emax{0,xjxi}}

任意边 <i,j> <script type="math/tex" id="MathJax-Element-23"> </script>如果 i 不在而j在,是允许的,而 i j不在是不允许的。
由于系数不可为负,变形有
max{wi>0max{0,1xi}wiwi<0max{0,1xi}(wi)<i,j>Emax{0,xjxi}}

wi>0wimin{wi>0max{0,xi}wi+wi<0max{0,1xi}(wi)+<i,j>Emax{0,xjxi}}

至于最大权闭合子图的节点数,显然就是 xi=0 的点数,即源点能访问到的点。
让我们抛弃画图理解,完全淹没在数学推导的叵测汪洋中吧!(?)

这是第几次没看数据范围开好数组就交题了。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,j,k) for(i=j;i<=k;++i)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f, N = 5005, M = 400005;

int level[N], cnt, cur[N], v[M], p[M], h[N], q[M], s, t;
ll w[M];
void add(int a, int b, ll c) {
    p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt;
    p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt;
}

bool bfs() {
    int f = 0, r = 0, u, i;
    memset(level, -1, sizeof level);
    q[r++] = s; level[s] = 1;
    while (f < r) {
        u = q[f++];
        for (i = h[u]; i; i = p[i]) {
            if (w[i] && level[v[i]] == -1) {
                level[v[i]] = level[u] + 1;
                q[r++] = v[i];
            }
        }
    }
    return level[t] != -1;
}

ll dfs(int u, ll low) {
    int i, tmp = 0; ll res = 0;
    if (u == t) return low;
    for (i = cur[u]; i && res < low; i = p[i]) {
        if (w[i] && level[v[i]] == level[u] + 1) {
            tmp = dfs(v[i], min(w[i], low - res));
            w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
            if (w[i]) cur[u] = i;
        }
    }
    if (!res) level[u] = -1;
    return res;
}

ll dinic() {
    ll ans = 0;
    while (bfs()) {
        for (int i = s; i <= t; ++i) cur[i] = h[i];
        ans += dfs(s, inf);
    }
    return ans;
}

void dfs(int x, int &ans) {
    level[x] = 1; ++ans;
    for (int i = h[x]; i; i = p[i])
        if (w[i] && !level[v[i]]) dfs(v[i], ans);
}

int main() {
    int i, b, c, n, m; ll a, ss;
    while (scanf("%d%d", &n, &m) == 2) {
        s = 0; t = n + 1; ss = 0; cnt = 1; memset(h, 0, sizeof h);
        FOR(i,1,n) {
            scanf("%lld", &a);
            if (a > 0) add(s, i, a), ss += a;
            else add(i, t, -a);
        }
        FOR(i,1,m) scanf("%d%d", &b, &c), add(b, c, inf);
        int ans1 = 0; ll ans2 = ss - dinic();
        memset(level, 0, sizeof level); dfs(s, ans1);
        printf("%d %lld\n", ans1 - 1, ans2);
    }
    return 0;
}

Firing

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 9537 Accepted: 2865

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

Source

POJ Monthly–2006.08.27, frkstyc

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值