# POJ 2987 Firing 题解【网络流】【最大权闭合子图】

## 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 $latex n$ $latex (0 < n\le 5000)$ and $latex m$ $latex (0\le m\le 60000)$ on the same line. Next follows $latex n+m$ lines. The first $latex n$ lines of these give the net profit/loss from firing the $latex i$-th employee individually $latex b_i$ $latex (|b_i|\le 10^7, 1\le i\le n)$. The remaining $latex m$ lines each contain two integers $latex i$ and $latex j$ $latex (1\le i,j\le n)$ meaning the $latex i$-th employee has the $latex 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 $latex 4$ and $latex 5$ will produce a net profit of $latex 2$, which is maximum.

## Source

POJ Monthly–2006.08.27, frkstyc

## 题解：

$latex S_2-F$同理。

## Code：

#include<cstdio>
#include<cstring>
int Min(int x,int y)
{
return x<y?x:y;
}
struct edge
{
int n,nxt,v;
edge(int n,int nxt,int v)
{
this->n=n;
this->nxt=nxt;
this->v=v;
}
edge(){}
}e[150000];
{
}
int d[5050],q[5050];
bool bfs()
{
int l=0,r=0;
memset(d,0,sizeof(d));
q[++r]=5001;
d[5001]=1;
while(l<r)
{
int x=q[++l];
if(e[i^1].v&&!d[e[i].n])
{
d[e[i].n]=d[x]+1;
q[++r]=e[i].n;
}
}
return d[0]>0;
}
int dinic(int x,int in)
{
if(x==5001)
return in;
int flow=in;
if(e[i].v&&d[e[i].n]==d[x]-1)
{
int tmp=dinic(e[i].n,Min(e[i].v,flow));
if(!tmp)
d[e[i].n]=0;
e[i].v-=tmp;
e[i^1].v+=tmp;
flow-=tmp;
}
return in-flow;
}
long long ans=0,cnt=-1;
bool used[5050];
void dfs(int x)
{
++cnt;
used[x]=1;
if(e[i].v&&!used[e[i].n])
dfs(e[i].n);
}
int main()
{
int n,m,u,v;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
{
scanf("%d",&u);
if(u<0)
else
{
ans+=u;
}
}
for(int i=1;i<=m;++i)
{
scanf("%d%d",&u,&v);
}
while(bfs())
{
int tmp=dinic(0,0x3fffffff);
while(tmp)
{
ans-=tmp;
tmp=dinic(0,0x3fffffff);
}
}
dfs(0);
printf("%lld %lld\n",cnt,ans);
return 0;
}


### 2 说点什么

0 Followers

Most reacted comment
0 Comment authors
Recent comment authors
Subscribe