POJ_2987_Firing_(最大流+最大权闭合图)

描述


http://poj.org/problem?id=2987

要炒员工鱿鱼,炒了一个人,他的下属一定被炒.给出每个人被炒后公司的收益(负值表示亏损),问怎样炒公司收益最大,以及这种方法炒了几个人.(先输出人数)

Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 9674   Accepted: 2906

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 ≤ in). The remaining m lines each contain two integers i and j (1 ≤ i, jn) 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.

Source

分析


求最大收益是用最大权闭合图.要炒一个人,他的下属也要被炒,那么边由上司连向下属,求最大权闭合图即可.

求炒了几个人实际是就是求先前求出来的那个最大权闭合图中点的个数.最大权闭合图对应的是最小割,而最小割中的边都是满流的,所以从源点出发无法到达闭合图的补集(即T),并且由于是闭合图,所以图中的边都不属于割,这样闭合图中就没有流量,从源点出发可以到达闭合图中的每一个点.

注意:

1.要用long long.

ps.f[S,T]=|f|,即整个图的流等于流过割的流,这样这道题中,f=fmax=cmin,又f=f[S,T],所以f[S,T]=cmin,也就是流过最小割的流等于最小割的容量,所以满流了.

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define rep(i,n) for(int i=0;i<(n);i++)
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getnum()
#define ll long long
using namespace std; const int maxn=+,INF=0x7fffffff;
int n,m;
ll sumw;
int level[maxn],iter[maxn];
bool vis[maxn];
struct edge
{
int to,cap,rev;
edge() {}
edge(int a,ll b,int c) : to(a),cap(b),rev(c) {}
};
vector <edge> g[maxn]; inline int getnum()
{
int r=,k=; char c;
for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;
for(;c>=''&&c<='';c=getchar()) r=r*+c-'';
return r*k;
} void add_edge(int from,int to,int cap)
{
g[from].push_back(edge(to,cap,g[to].size()));
g[to].push_back(edge(from,,g[from].size()-));
} void bfs(int s)
{
CC(level,-);
level[s]=;
queue <int> q;
q.push(s);
while(!q.empty())
{
int t=q.front(); q.pop();
rep(i,g[t].size())
{
edge e=g[t][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[t]+;
q.push(e.to);
}
}
}
} int dfs(int v,int t,int f)
{
if(v==t) return f;
for(int &i=iter[v];i<g[v].size();i++)
{
edge &e=g[v][i];
if(e.cap>&&level[e.to]>level[v])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap-=d;
g[e.to][e.rev].cap+=d;
return d;
}
} }
return ;
} ll max_flow(int s,int t)
{
ll flow=;
bfs(s);
while(level[t]>)
{
int f;
CC(iter,);
while((f=dfs(s,t,INF))>) flow+=f;
bfs(s);
}
return flow;
} int DFS(int v)
{
vis[v]=true;
int ans=;
rep(i,g[v].size())
{
edge e=g[v][i];
if(!vis[e.to]&&e.cap>) ans+=DFS(e.to);
}
return ans;
} void init()
{
read(n); read(m);
for1(i,,n)
{
int w; read(w);
if(w>)
{
sumw+=w;
add_edge(,i,w);
}
else
{
add_edge(i,n+,-w);
}
}
for1(i,,m)
{
int a,b; read(a); read(b);
add_edge(a,b,INF);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("firing.in","r",stdin);
freopen("firing.out","w",stdout);
#endif
init();
ll ans=sumw-max_flow(,n+);
printf("%d %lld\n",DFS()-,ans);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("firing.out");
#endif
return ;
}
 
上一篇:Error:(108) No resource identifier found for attribute 'style' in package 'android'


下一篇:6.cadence原理图下[原创]