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

 【题意】

 

 

 【分析】

这个题就是经典的二者选其一中的选/不选问题

 

挺神奇的建法

 

【代码】

 

#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define lson now<<1
#define rson now<<1|1
typedef long long ll;
const int maxn=205;
const int maxm=40005;
int id[maxn][maxn],cnt;
int val[maxn][maxn],n,m;
int S,T;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
const ll inf=1e17;
int head[10005],tot=1,cur[10005];
struct edge
{
    int to,nxt;
    ll v;
}e[maxm<<1];
void add(int x,int y,ll z)
{
    e[++tot].to=y; e[tot].nxt=head[x]; e[tot].v=z; head[x]=tot;
    e[++tot].to=x; e[tot].nxt=head[y]; e[tot].v=0; head[y]=tot;
}
int dep[10005];
bool bfs()
{
    for(int i=S;i<=T;i++)
        dep[i]=-1,cur[i]=head[i];
    // memset(dep,-1,sizeof(dep));
    // memcpy(cur,head,sizeof(cur));
    queue <int> q;
    dep[S]=0;
    q.push(S);
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=head[u];i;i=e[i].nxt)
        {
            int to=e[i].to;
            if(dep[to]!=-1 || !e[i].v) continue;
            q.push(to);
            dep[to]=dep[u]+1;
        }
    }
    return (dep[T]!=-1);
}
ll dfs(int u,ll flow)
{
    if(u==T) return flow;
    ll res=0;
    for(int &i=cur[u];i;i=e[i].nxt)
    {
        int to=e[i].to;
        if(dep[to]!=dep[u]+1 || e[i].v<=0) continue;
        ll tmp=dfs(to,min(e[i].v,flow));
        flow-=tmp; res+=tmp;
        e[i].v-=tmp; e[i^1].v+=tmp;
        if(!flow) break;
    }
    if(!res) dep[u]=-1;
    return res;
}
ll ans;
ll dinic()
{
    ll ans=0;
    while(bfs())
    {
        ans+=dfs(S,inf);
    }
    return ans;
}
int main()
{
    // freopen("a.in","r",stdin);
    // freopen("a.out","w",stdout);
    scanf("%d",&n);
    S=0; T=n+1;
    ll sum=0,x;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&x);
        add(i,T,x);
    }
    for(int i=1;i<=n;i++)
    {
        ll he=0;
        for(int j=1;j<=n;j++)
        {
            scanf("%lld",&x);
            if(x) add(i,j,x<<1);
            he+=x;
        }
        sum+=he;
        add(S,i,he);
    }
    printf("%lld",sum-dinic());
    return 0;
}

 

posted @ 2021-06-03 15:14  andyc_03  阅读(57)  评论(0)    收藏  举报