zoukankan      html  css  js  c++  java
  • 【洛谷P1791】人员雇佣

    题目

    题目链接:https://www.luogu.com.cn/problem/P1791

    作为一个富有经营头脑的富翁,小 L 决定从本国最优秀的经理中雇佣一些来经营自己的公司。这些经理相互之间合作有一个贡献指数,(我们用 (E_{i,j}) 表示 (i) 经理对 (j) 经理的了解程度),即当经理 (i) 和经理 (j) 同时被雇佣时,经理 (i) 会对经理 (j) 做出贡献,使得所赚得的利润增加 (E_{i,j})
    当然,雇佣每一个经理都需要花费一定的金钱 (A_i),对于一些经理可能他做出的贡献不值得他的花费,那么作为一个聪明的人,小 L 当然不会雇佣他。 然而,那些没有被雇佣的人会被竞争对手所雇佣,这个时候那些人会对你雇佣的经理的工作造成影响,使得所赚得的利润减少 (E_{i,j})(注意:这里的 (E_{i,j}) 与上面的 (E_{i,j}) 是同一个)。
    作为一个效率优先的人,小 L 想雇佣一些人使得净利润最大。你可以帮助小L解决这个问题吗?
    (nleq 1000)

    思路

    考虑一个最大权闭合子图模型,对于每一个人,从源点连一条流量为 (a_i) 的边,向汇点连一条权值为 (sum^{n}_{j=1}E_{i,j}) 的边。前者表示选这一个人,后者表示不选这一个人。
    接下来考虑两个人之间的贡献。如果 (i) 被选了且 (j) 没被选,那么会产生 (-E_{i,j}) 的贡献。那么就从点 (i) 向点 (j) 连一条流量为 (2E_{i,j}) 的边即可。
    最后用 (sum^{n}_{i=1}sum^{n}_{j=1}E_{i,j}) 减去最大流就是答案。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int N=1010,M=2100010,Inf=1e9;
    int n,S,T,tot=1,head[N],cur[N],dep[N],a[N],b[N][N],s[N];
    ll ans,maxf;
    
    struct edge
    {
    	int next,to,flow;
    }e[M];
    
    void add(int from,int to,int flow)
    {
    	e[++tot]=(edge){head[from],to,flow};
    	head[from]=tot;
    	swap(from,to);
    	e[++tot]=(edge){head[from],to,0};
    	head[from]=tot;
    }
    
    bool bfs()
    {
    	memset(dep,0x3f3f3f3f,sizeof(dep));
    	memcpy(cur,head,sizeof(head));
    	queue<int> q;
    	q.push(S); dep[S]=0;
    	while (q.size())
    	{
    		int u=q.front(); q.pop();
    		for (int i=head[u];~i;i=e[i].next)
    		{
    			int v=e[i].to;
    			if (e[i].flow && dep[v]>dep[u]+1)
    			{
    				dep[v]=dep[u]+1;
    				q.push(v);
    			}
    		}
    	}
    	return dep[T]<Inf;
    }
    
    int dfs(int x,int flow)
    {
    	if (x==T) return flow;
    	int used=0,res;
    	for (int i=cur[x];~i;i=e[i].next)
    	{
    		int v=e[i].to; cur[x]=i;
    		if (e[i].flow && dep[v]==dep[x]+1)
    		{
    			res=dfs(v,min(flow-used,e[i].flow));
    			e[i].flow-=res; e[i^1].flow+=res; used+=res;
    			if (used==flow) return used;
    		}
    	}
    	return used;
    }
    
    void dinic()
    {
    	while (bfs()) maxf+=dfs(S,Inf);
    }
    
    int main()
    {
    	memset(head,-1,sizeof(head));
    	S=N-1; T=N-2;
    	scanf("%d",&n);
    	for (int i=1;i<=n;i++)
    	{
    		scanf("%d",&a[i]);
    		add(S,i,a[i]);
    	}
    	for (int i=1;i<=n;i++)
    		for (int j=1;j<=n;j++)
    		{
    			scanf("%d",&b[i][j]);
    			if (b[i][j]) add(i,j,2*b[i][j]);
    			s[i]+=b[i][j]; ans+=b[i][j];
    		}
    	for (int i=1;i<=n;i++)
    		add(i,T,s[i]);
    	dinic();
    	cout<<ans-maxf;
    	return 0;
    }
    
  • 相关阅读:
    hdu 4183(网络流)
    hdu 1565&hdu 1569(网络流--最小点权值覆盖)
    hdu 1532(最大流)
    HDU 2141 Can you find it?
    HDU 1096 A+B for Input-Output Practice (VIII)
    HDU 1095 A+B for Input-Output Practice (VII)
    HDU 1094 A+B for Input-Output Practice (VI)
    HDU 1093 A+B for Input-Output Practice (V)
    HDU 1092 A+B for Input-Output Practice (IV)
    HDU 1091 A+B for Input-Output Practice (III)
  • 原文地址:https://www.cnblogs.com/stoorz/p/15015813.html
Copyright © 2011-2022 走看看