poj 2987 Firing
Posted ZefengYao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2987 Firing相关的知识,希望对你有一定的参考价值。
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 10696 | Accepted: 3226 |
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
最大权闭合图的求解方法是
-
先构造网络流N,添加源点s,从s到正权值点做一条边,容量为点的权值。
-
添加汇点t,从负权值点到t做一条边,容量为点的权值的绝对值。
-
原来的边的容量统统设为无穷大。
-
求解最小割,最大权=正权值之和-最小割权值
-
残余网络中的点的个数即为裁员个数。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<queue> #include<set> #include<vector> #include<cstring> #include<string> #define INF 0x3f3f3f3f3f3f3f3f using namespace std; const int N_MAX = 5000+4, M_MAX = 200000, V_MAX = N_MAX + 2; typedef long long ll; int n, m; struct edge { int to; ll cap;int rev; edge(int to, ll cap, int rev) :to(to), cap(cap), rev(rev) {} }; vector<edge>G[V_MAX]; int level[V_MAX]; int iter[V_MAX]; void add_edge(int from, int to, ll cap) { G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int>que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge&e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } ll dfs(int v, int t, ll f) { if (v == t)return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge&e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { ll d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(int s, int t) { ll flow = 0; for (;;) { bfs(s); if (level[t] < 0)return flow; memset(iter, 0, sizeof(iter)); ll f; while ((f = dfs(s, t, INT_MAX))>0) { flow += f; } } } int res = 0; bool vis[V_MAX]; void solve(int v) {//深搜找点 res++; vis[v] = true; for (int i = 0; i < G[v].size();i++) { edge e = G[v][i]; if (e.cap > 0 && !vis[e.to]) { solve(e.to); } } } int main() { while (scanf("%d%d",&n,&m)!=EOF) { res = 0; memset(vis, 0, sizeof(vis)); int s = 0, t = n+1,V=t+1; ll sum = 0; for (int i = 0; i < n;i++) { ll a; scanf("%lld",&a); if (a > 0) { add_edge(s, i + 1, a); sum += a; } else add_edge(i + 1, t, -a); } for (int i = 0; i < m;i++) { int a, b; scanf("%d%d",&a,&b); add_edge(a,b,INF); } ll flow=max_flow(s, t); solve(s); printf("%d %lld\n",--res,sum-flow); for (int i = 0; i < V;i++) {//向量清空 G[i].clear(); } } return 0; }
以上是关于poj 2987 Firing的主要内容,如果未能解决你的问题,请参考以下文章