BZOJ2039: [2009国家集训队]employ人员雇佣

19 篇文章 0 订阅

经典最小割
先将所有收益E加到答案
源代表不雇佣,汇代表雇佣
每个点向汇连边流量为雇佣价格

对于每对关系i,j之间,
如果两个都不雇佣,损失为2e
如果雇佣i不雇佣j,损失为fi(雇佣花费)+2e
如雇佣j不雇佣i,损失为fj+2e
两个都雇佣,损失为fi+fj

于是源向i,j连边流量为ei,j
i向j,j向i连边,流量为2e

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

inline void read(int &x)
{
    char c;
    while(!((c=getchar())>='0'&&c<='9'));
    x=c-'0';
    while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';
}
const int maxn = 2100;
const int maxm = 410000;

int n,m,st,ed;

struct edge
{
    int y,nex;
    int c;
}a[maxm]; int len,fir[maxn],fi[maxn];

void ins(const int x,const int y,const int c1,const int c2)
{
    a[++len].y=y; a[len].c=c1;
    a[len].nex=fir[x]; fir[x]=len;
    a[++len].y=x; a[len].c=c2;
    a[len].nex=fir[y]; fir[y]=len;
}
queue<int>q;
int h[maxn];
bool bfs()
{
    for(int i=1;i<=ed;i++) h[i]=0;
    q.push(st); h[st]=1;
    while(!q.empty())
    {
        const int x=q.front(); q.pop();
        for(int k=fir[x];k;k=a[k].nex)
        {
            const int y=a[k].y;
            if(a[k].c&&!h[y])
            {
                h[y]=h[x]+1;
                q.push(y);
            }
        }
    }
    return h[ed]>0;
}
int dfs(const int x,const int flow)
{
    if(x==ed) return flow;
    int delta=0;
    for(int k=fir[x];k;k=a[k].nex)
    {
        const int y=a[k].y;
        if(a[k].c&&h[y]==h[x]+1)
        {
            int mink=dfs(y,min(a[k].c,flow-delta));
            delta+=mink;
            a[k].c-=mink; a[k^1].c+=mink;
        }
        if(delta==flow) return flow;
    }
    if(!delta) h[x]=0;
    return delta;
}


int main()
{
    len=1; //memset(fir,0,sizeof fir);
    read(n); st=n+1,ed=st+1;
    for(int i=1;i<=n;i++)
    {
        int x; read(x);
        ins(st,i,x,0);
    }

    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int ai=0;
        for(int j=1;j<=n;j++)
        {
            int x; read(x);
            if(x)
            {
                ai+=x;
                ans+=x;
                if(i<j) ins(i,j,2*x,2*x);
            }
        }
        ins(i,ed,ai,0);
    }

    while(bfs())
    {
        //for(int i=1;i<=ed;i++) fi[i]=fir[i];
        ans-=dfs(st,INT_MAX);
    }
    printf("%d\n",ans);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值