[BZOJ2406]矩阵(二分+有源汇有上下界的可行流)

题目描述

传送门

题解

刚开始没看见绝对值。。。
把这道题翻译一下其实就是构造一个b矩阵,其中每一个点有限制[L,R],令矩阵c=a-b,使c矩阵每一行的和的绝对值和每一列的和的绝对值的最大值最小

最大值最小很容易想到二分
二分答案mid之后,用网络流判定
就是满足 |aibi|mid
分类讨论一下得出 aimidbiai+mid
然后就很容易看出是一个有上下界的网络流了
原图:
每一行和每一列建一个点xi,yi
s->xi,[ai-mid,ai+mid]
yj->t,[aj-mid,aj+mid]
xi->yj,[L,R]
只要判断是否有可行流就行了
按照有源汇有上下界的可行流将原图改造求解即可

代码

#include
#include
#include
#include
#include
#include
using namespace std;
#define N 410
#define E 100005
#define inf 2000000000

int n,m,s,t,ss,tt,L,R,in,out,maxflow,ans;
int a[N][N],line[N],lie[N];
int tot,point[N],nxt[E],v[E],remain[E];
int d[N],deep[N],last[N],num[N],cur[N];
queue <int> q;

void addedge(int x,int y,int cap)
{
    ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;
    ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
void bfs(int t)
{
    for (int i=1;i<=t;++i) deep[i]=t;
    deep[t]=0;
    for (int i=1;i<=t;++i) cur[i]=point[i];
    while (!q.empty()) q.pop();
    q.push(t);
    while (!q.empty())
    {
        int now=q.front();q.pop();
        for (int i=point[now];i!=-1;i=nxt[i])
            if (deep[v[i]]==t&&remain[i^1])
            {
                deep[v[i]]=deep[now]+1;
                q.push(v[i]);
            }
    }
}
int addflow(int s,int t)
{
    int ans=inf,now=t;
    while (now!=s)
    {
        ans=min(ans,remain[last[now]]);
        now=v[last[now]^1];
    }
    now=t;
    while (now!=s)
    {
        remain[last[now]]-=ans;
        remain[last[now]^1]+=ans;
        now=v[last[now]^1];
    }
    return ans;
}
void isap(int s,int t)
{
    bfs(t);
    for (int i=1;i<=t;++i) ++num[deep[i]];

    int now=s;
    while (deep[s]if (now==t)
        {
            maxflow+=addflow(s,t);
            now=s;
        }

        bool has_find=false;
        for (int i=cur[now];i!=-1;i=nxt[i])
            if (deep[v[i]]+1==deep[now]&&remain[i])
            {
                has_find=true;
                cur[now]=i;
                last[v[i]]=i;
                now=v[i];
                break;
            }

        if (!has_find)
        {
            int minn=t-1;
            for (int i=point[now];i!=-1;i=nxt[i])
                if (remain[i]) minn=min(minn,deep[v[i]]);
            if (!(--num[deep[now]])) break;
            ++num[deep[now]=minn+1];
            cur[now]=point[now];
            if (now!=s) now=v[last[now]^1];
        }
    }
}
bool check(int mid)
{
    tot=-1;memset(point,-1,sizeof(point));
    memset(num,0,sizeof(num));
    memset(d,0,sizeof(d));
    in=out=0;maxflow=0;
    for (int i=1;i<=n;++i)
    {
        addedge(s,i,mid+mid);
        d[s]-=line[i]-mid,d[i]+=line[i]-mid;
    }
    for (int i=1;i<=m;++i)
    {
        addedge(n+i,t,mid+mid);
        d[n+i]-=lie[i]-mid,d[t]+=lie[i]-mid;
    }
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
        {
            addedge(i,n+j,R-L);
            d[i]-=L,d[n+j]+=L;
        }
    addedge(t,s,inf);
    for (int i=1;i<=t;++i)
    {
        if (d[i]>0) addedge(ss,i,d[i]),in+=d[i];
        if (d[i]<0) addedge(i,tt,-d[i]),out-=d[i];
    }
    if (in!=out) return 0;
    isap(ss,tt);
    return maxflow==in;
}
int find()
{
    int l=0,r=2000000,mid,ans=2000000;
    while (l<=r)
    {
        mid=(l+r)>>1;
        if (check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    return ans;
}
int main()
{
    scanf("%d%d",&n,&m);
    s=n+m+1,t=s+1,ss=t+1,tt=ss+1;
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
        {
            scanf("%d",&a[i][j]);
            line[i]+=a[i][j];
            lie[j]+=a[i][j];
        }
    scanf("%d%d",&L,&R);
    ans=find();
    printf("%d\n",ans);
}

你可能感兴趣的:(题解,网络流,二分)