您的位置:首页 > 其它

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

2016-04-09 19:42 459 查看
最大权闭合子图。。。闭合子图满足在子图内的任意点在原图中存在出边总是指向该闭合子图。

如果添加了超级源汇,那么这个图由一个割被分成了两个集合[S,T],且T是闭合子图。

最小割!

让我们来推导一下最大权闭合子图的解决方法。

令xi表示点i是否在闭合子图中,0在,1不在。

那么答案很显然了:

max{∑max{0,1−xi}⋅wi−∑<i,j>∈Emax{0,xj−xi}⋅∞}

任意边<i,j>如果i不在而j在,是允许的,而i在j不在是不允许的。

由于系数不可为负,变形有

max{∑wi>0max{0,1−xi}⋅wi−∑wi<0max{0,1−xi}⋅(−wi)−∑<i,j>∈Emax{0,xj−xi}⋅∞}

∑wi>0wi−min{∑wi>0max{0,xi}⋅wi+∑wi<0max{0,1−xi}⋅(−wi)+∑<i,j>∈Emax{0,xj−xi}⋅∞}

至于最大权闭合子图的节点数,显然就是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
, cnt, cur
, v[M], p[M], h
, 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: