正权点之和减去最小割。
例题:Firing POJ - 2987:
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.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define ll long long
#define llu unsigned ll
using namespace std;
const int maxx=5010;
const int maxn=65010;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
int head[maxx],ver[maxn<<1],nt[maxn<<1];
ll edge[maxn<<1],vi[maxx];
int d[maxx],n,m,s,t,tot,cnt;
bool ha[maxx];
ll maxflow=0;
void add(int x,int y,ll z)
{
ver[++tot]=y,edge[tot]=z,nt[tot]=head[x],head[x]=tot;
ver[++tot]=x,edge[tot]=0,nt[tot]=head[y],head[y]=tot;
}
bool bfs(void) //在残量网络上构造分层图
{
memset(d,0,sizeof(d));
queue<int>q;
q.push(s);
d[s]=1;
while(q.size())
{
int x=q.front();
q.pop();
for(int i=head[x];i;i=nt[i])
{
int y=ver[i];
if(edge[i]&&!d[y])
{
q.push(y);
d[y]=d[x]+1;
if(y==t) return true;
}
}
}
return false;
}
ll dinic(int x,ll flow)//在当前分层图上增广
{
if(x == t) return flow;
ll rest =flow ,k;
for(int i=head[x];i&&rest;i=nt[i])
{
int y=ver[i];
if(edge[i]&&d[y]==d[x]+1)
{
k=dinic(y,min(rest,edge[i]));
if(!k) d[y]=0;//剪枝,去掉增广完毕的点
edge[i] -= k;
edge[i^1] += k;
rest -= k;
}
}
return flow - rest;
}
void dfs(int x)
{
ha[x]=true;
for(int i=head[x];i;i=nt[i])
{
int y=ver[i];
ll z=edge[i];
if(!z||ha[y]) continue;
cnt++;
dfs(y);
}
}
int main(void)
{
scanf("%d%d",&n,&m);
s=n+1,t=n+2;
tot=1;
int x,y;
ll sum=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&vi[i]);
if(vi[i]>=0) add(s,i,vi[i]),sum+=vi[i];
else add(i,t,-vi[i]);
}
for(int i=1;i<=m;i++)
scanf("%d%d",&x,&y),add(x,y,lnf);
ll flow=0;
while(bfs())
while(flow = dinic(s,lnf))
maxflow += flow;
dfs(s);
printf("%d %lld\n",cnt,sum-maxflow);
return 0;
}