Firing
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 11790 | Accepted: 3577 |
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
As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.
题意:
公司里又n个员工,他们之间有m段上下级的关系。每个员工都会有一个权值。现在公司要裁员,员工被裁掉的时候公司会获得其权值,但是当上司被裁掉之后,所有其下属也会被一起裁掉,现在问公司能够获得的最大利益是多少,同时获得该利益所需要裁掉的最少人数是多少。
做法:
最大权闭合子图,原理我就不多说了,大概就是最大权=sum-最小割,但是最小割=最大流。建图大概就是把所有权值为正的边与超级源点相连,权值为负的边与超级汇点相连,建立的都是单向边,跑一遍最大流,至于最少要多少人,再从超级源点做一次dfs,如果被最大流覆盖即跑满流,说明这个人是不能被裁掉的,而剩下来还有权值的那些点就是获得利益的来源,所以当搜索到还有流量不为0的,即这个人要被裁掉,ans++。
代码如下:
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
const int N = 5005;
const int inf = 999999999;
struct Edge{
int v,next;
int w;
}edge[N*N];
int head[N];
int level[N];
int tot,now;
void init(){
memset(head,-1,sizeof(head));
tot=0;
}
void add(int u,int v,int w){
edge[tot].v=v,edge[tot].w=w,edge[tot].next=head[u],head[u]=tot++;
edge[tot].v=u,edge[tot].w=0,edge[tot].next=head[v],head[v]=tot++;
}
int BFS(int sp,int tp){
queue<int >q;
memset(level,0,sizeof(level));
level[sp]=1;
q.push(sp);
while(!q.empty()){
int u=q.front();
q.pop();
if(u==tp) return 1;
for(int k=head[u];k!=-1;k=edge[k].next){
int v=edge[k].v;
int w=edge[k].w;
if(level[v]==0&&w!=0){
level[v]=level[u]+1;
q.push(v);
}
}
}
return -1;
}
int dfs(int u,int tp,int increaseRoad){
if(u==tp) return increaseRoad;
int ret=0;
for(int k=head[u];k!=-1;k=edge[k].next){
int v=edge[k].v;
int w=edge[k].w;
if(level[v]==level[u]+1&&w!=0){
int MIN=min(increaseRoad-ret,w);
w=dfs(v,tp,MIN);
edge[k].w-=w;
edge[k^1].w+=w;
ret+=w;
if(ret==increaseRoad) return ret;
}
}
return ret;
}
ll Dinic(int sp,int tp){
ll ans = 0;
while(BFS(sp,tp)!=-1) ans+=(ll)dfs(sp,tp,inf*1.0);
return ans;
}
int ans=0;
bool vis[N];
void dfs(int u){
ans++;
vis[u]=true;
for(int k=head[u];k!=-1;k=edge[k].next){
if(edge[k].w>0&&!vis[edge[k].v]){
dfs(edge[k].v);
}
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
int sp=0,tp=n+1;
ll sum=0;
for(int i=1;i<=n;i++){
int w;
scanf("%d",&w);
if(w<0) add(i,tp,-w);
else add(sp,i,w),sum+=w;
}
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v,inf);
}
ll MAX=sum-Dinic(sp,tp);
memset(vis,false,sizeof(vis));
dfs(sp);
ans-=1;
printf("%d %lld\n",ans,MAX);
}
}