poj2987 Firing
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 11132 | Accepted: 3372 |
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
Source
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const ll maxn = 100010,inf = 0x7fffffff; ll n,f,d,S,T,head[maxn],to[maxn],nextt[maxn],tot = 2,w[maxn],ans,m,c[maxn],sum,flag[maxn]; ll vis[maxn],ans2; void add(ll x,ll y,ll z) { w[tot] = z; to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; w[tot] = 0; to[tot] = x; nextt[tot] = head[y]; head[y] = tot++; } bool bfs() { queue <ll> q; memset(vis,-1,sizeof(vis)); vis[S] = 0; q.push(S); while (!q.empty()) { ll u = q.front(); q.pop(); if (u == T) return true; for (int i = head[u];i;i = nextt[i]) { ll v = to[i]; if (vis[v] == -1 && w[i]) { vis[v] = vis[u] + 1; q.push(v); } } } return false; } ll dfs(ll u,ll f) { ll res = 0; if (u == T) return f; for (int i = head[u];i;i = nextt[i]) { ll v = to[i]; if (w[i] && vis[v] == vis[u] + 1) { ll temp = dfs(v,min(f - res,w[i])); res += temp; w[i] -= temp; w[i ^ 1] += temp; if (res == f) return res; } } if (!res) vis[u] = -1; return res; } void dfs2(ll u) { flag[u] = 1; ans2++; for (int i = head[u];i;i = nextt[i]) { ll v = to[i]; if (w[i] && !flag[v]) { flag[v] = 1; dfs2(v); } } } void dinic() { while (bfs()) ans += dfs(S,inf); } int main() { scanf("%lld%lld",&n,&m); S = 0; T = n + 1; for (int i = 1; i <= n; i++) { scanf("%lld",&c[i]); if (c[i] > 0) { sum += c[i]; add(S,i,c[i]); } if (c[i] < 0) add(i,T,-c[i]); } for (int i = 1; i <= m; i++) { int a,b; scanf("%d%d",&a,&b); add(a,b,inf); } dinic(); dfs2(S); printf("%lld %lld\n",ans2 - 1,sum - ans); return 0; }