POJ 2987 Firing(最大权闭合图)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/81865369

                                                                                              Firing

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 11800   Accepted: 3579

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 ≤ ij ≤ 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.

扫描二维码关注公众号,回复: 2950902 查看本文章

题目大意:公司官僚成风,盘根错节,办实事的码农没几个。老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误。给出每个人的贡献值和从属关系,求最小裁员数及最大贡献值和。

最大权闭合图的模板题,存个模板

答案=正权点权值和-最大流

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=5010;
const int maxm=6e4+7;
const ll inf=0x3f3f3f3f3f3f3f3fll;
struct Node
{
    int to;
    ll capa;
    int next;
}edge[maxm<<4];
struct LDJ
{
    int x;
    int y;
}point[maxm];
int source,sink;
int cnt;
int human_cnt;
int head[maxn];
bool vis[maxn];
int dep[maxn];
bool cut_vis[maxn];
void init()
{
    memset(head,-1,sizeof(head));
    memset(cut_vis,false,sizeof(cut_vis));
    cnt=0;
    return;
}
void add(int u,int v,ll capa)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool bfs()
{
	queue<int> que;
	que.push(source);
	memset(dep,-1,sizeof(dep));
	dep[source]=0;
	while(!que.empty())
	{
		int node=que.front();
		que.pop();
		for(int i=head[node];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].capa>0&&dep[v]==-1)
			{
				dep[v]=dep[node]+1;
				if(v==sink) return true;
				que.push(v);
			}
		}
	}
	return dep[sink]!=-1;
}
ll dfs(int node,ll minn)
{
	if(node==sink||minn==0)
	{
		return minn;
	}
	ll r=0;
	for(int i=head[node];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(edge[i].capa>0&&dep[v]==dep[node]+1)
		{
			ll tmp=dfs(v,min(edge[i].capa,minn));
			if(tmp>0)
			{
				edge[i].capa-=tmp;
				edge[i^1].capa+=tmp;
				r+=tmp;
				minn-=tmp;
				if(!minn) break;
			}
		}
	}
	if(!r) dep[node]=-1;
	return r;
}
ll dinic()
{
	ll maxflow=0;
	while(bfs())
	{
		maxflow+=dfs(source,inf);
	}
	return maxflow;
}
void cut_dfs(int node)
{
    vis[node]=true;
    for(int i=head[node];~i;i=edge[i].next)
    {
        if(edge[i].capa>0&&!vis[edge[i].to])
        {
            human_cnt++;
            cut_dfs(edge[i].to);
        }
    }
    return;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        source=0;
        sink=n+1;
        ll sum=0;
        for(int i=1;i<=n;i++)
        {
            ll x;
            scanf("%lld",&x);
            if(x>0)
            {
                add(source,i,x);
                sum+=x;
            }
            else
            {
                add(i,sink,-x);
            }
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&point[i].x,&point[i].y);
            add(point[i].x,point[i].y,inf);
        }
        ll ans=dinic();
        human_cnt=0;
        cut_dfs(source);
        printf("%d %lld\n",human_cnt,sum-ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81865369