POJ2987-Firing(最大权闭合图)

来源:互联网 发布:淘宝直播在哪里开通 编辑:程序博客网 时间:2024/05/21 19:45

Firing
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 10904 Accepted: 3290

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 58-9-2012-101 22 51 43 44 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


题意:给出每个员工的价值(有的人是负的)和员工的直属关系,裁掉领导就必须裁下属,可能成环,问怎样裁员才能得到最大利益,如果有多解,输出裁员人数最少的那一个。

解题思路:根据裁员的这些性质,其实就是求最大权闭合图问题。

最大权闭合图的求解方法是:先构造网络流N,添加源点s,从s到正权值点做一条边,容量为点的权值。添加汇点t,从负权值点到t做一条边,容量为点的权值的绝对值。原来的边的容量统统设为无穷大。求解最小割,最大权 = 正权值之和 - 最小割权值。根据最小割的性质,被裁掉的人肯定不在最小割中,所以只要从源点开始对残留网络dfs,能访问到的点就删去



#include <iostream>  #include <cstdio>  #include <cstring>  #include <string>  #include <algorithm>  #include <cmath>  #include <map>  #include <set>  #include <stack>  #include <queue>  #include <vector>  #include <bitset>  using namespace std;#define LL long long  const LL INF = 0x3f3f3f3f3f3f3f3f;#define MAXN 5009   struct node{int u, v, next;LL cap;} edge[MAXN*MAXN];int nt[MAXN], s[MAXN], d[MAXN],vis[MAXN];int cnt,res;void init(){cnt = 0;memset(s, -1, sizeof(s));}void add(int u, int v, LL c){edge[cnt].u = u;edge[cnt].v = v;edge[cnt].cap = c;edge[cnt].next = s[u];s[u] = cnt++;edge[cnt].u = v;edge[cnt].v = u;edge[cnt].cap = 0;edge[cnt].next = s[v];s[v] = cnt++;}bool BFS(int ss, int ee){memset(d, 0, sizeof d);d[ss] = 1;queue<int>q;q.push(ss);while (!q.empty()){int pre = q.front();q.pop();for (int i = s[pre]; ~i; i = edge[i].next){int v = edge[i].v;if (edge[i].cap > 0 && !d[v]){d[v] = d[pre] + 1;q.push(v);}}}return d[ee];}LL DFS(int x, LL exp, int ee){if (x == ee || !exp) return exp;LL temp, flow = 0;for (int i = nt[x]; ~i; i = edge[i].next, nt[x] = i){int v = edge[i].v;if (d[v] == d[x] + 1 && (temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0){edge[i].cap -= temp;edge[i ^ 1].cap += temp;flow += temp;exp -= temp;if (!exp) break;}}if (!flow) d[x] = 0;return flow;}LL Dinic_flow(int ss, int ee){LL ans = 0;while (BFS(ss, ee)){for (int i = 0; i <= ee; i++) nt[i] = s[i];ans += DFS(ss, INF, ee);}return ans;}void dfs(int k){res++;vis[k] = 1;for (int i = s[k]; ~i; i = edge[i].next)if (!vis[edge[i].v] && edge[i].cap) dfs(edge[i].v);}int main(){int n, m,w;while (~scanf("%d %d", &n, &m)){init();LL sum = 0;int ss = 0, ee = n + 1,u,v;for (int i = 1; i <= n; i++){scanf("%d", &w);if (w > 0) { sum += w, add(ss, i, w); }if (w < 0) add(i, ee, -w);}for (int i = 0; i < m; i++){scanf("%d%d", &u, &v);add(u, v, INF);}LL ma = sum - Dinic_flow(ss,ee);memset(vis, 0, sizeof vis);res = 0;dfs(ss);printf("%d %lld\n",--res,ma);}return 0;}

原创粉丝点击