[POJ2396]Budget(有源汇有上下界的可行流)

题目描述

传送门

题解

和无源汇有上下界的可行流类似,只需要加一条边t->s,限制为[0,inf]就可以了,相当于是让源点和汇点也满足流量平衡
然后再建立附加源汇ss,tt,其余的就和无源汇的一样了

这道题的话,把每一行看成一个点xi,每一列看成一个点yi,那么首先s->xi,[sumxi,sumxi],yi->t,[sumyi,sumyi]
对于某一个点(i,j)的限制[l,r],xi->yj,[l,r]

代码

#include
#include
#include
#include
#include
#include
using namespace std;
#define N 10005
#define inf 1000000000

char opt;
int T,n,m,k,x,y,z,s,t,ss,tt,maxflow,in,out;
int l[205][205],r[205][205],d[N],pipe[205][205];
int tot,point[N],nxt[N],v[N],remain[N];
int deep[N],last[N],cur[N],num[N];
queue <int> q;

void clear()
{
    for (int i=1;i<=n;++i)
        for (int j=1;j<=m;++j)
            l[i][j]=0,r[i][j]=inf;
    maxflow=in=out=0;
    memset(d,0,sizeof(d));
    tot=-1;memset(point,-1,sizeof(point));
    memset(num,0,sizeof(num));
}
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 now=t,ans=inf;
    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];
        }
    }
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        clear();
        s=n+m+1,t=s+1,ss=t+1,tt=ss+1;
        for (int i=1;i<=n;++i) scanf("%d",&x),d[s]-=x,d[i]+=x;
        for (int i=1;i<=m;++i) scanf("%d",&x),d[n+i]-=x,d[t]+=x;
        scanf("%d",&k);
        while (k--)
        {
            scanf("%d %d %c %d",&x,&y,&opt,&z);
            if (!x&&!y)
                for (int i=1;i<=n;++i)
                    for (int j=1;j<=m;++j)
                    {
                        if (opt=='=') l[i][j]=r[i][j]=z;
                        if (opt=='>') l[i][j]=max(l[i][j],z+1);
                        if (opt=='<') r[i][j]=min(r[i][j],z-1);
                    }
            else if (!x)
                for (int i=1;i<=n;++i)
                {
                    if (opt=='=') l[i][y]=r[i][y]=z;
                    if (opt=='>') l[i][y]=max(l[i][y],z+1);
                    if (opt=='<') r[i][y]=min(r[i][y],z-1);
                }
            else if (!y)
                for (int i=1;i<=m;++i)
                {
                    if (opt=='=') l[x][i]=r[x][i]=z;
                    if (opt=='>') l[x][i]=max(l[x][i],z+1);
                    if (opt=='<') r[x][i]=min(r[x][i],z-1);
                }
            else
            {
                if (opt=='=') l[x][y]=r[x][y]=z;
                if (opt=='>') l[x][y]=max(l[x][y],z+1);
                if (opt=='<') r[x][y]=min(r[x][y],z-1);
            }
        }
        addedge(t,s,inf);
        for (int i=1;i<=n;++i)
            for (int j=1;j<=m;++j)
            {
                addedge(i,n+j,r[i][j]-l[i][j]),pipe[i][j]=tot;
                d[i]-=l[i][j],d[n+j]+=l[i][j];
            }
        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) {puts("IMPOSSIBLE");puts("");continue;}
        isap(ss,tt);
        if (maxflow==in)
        {
            for (int i=1;i<=n;++i)
                for (int j=1;j<=m;++j)
                    printf("%d%c",remain[pipe[i][j]]+l[i][j]," \n"[j==m]);
        }
        else puts("IMPOSSIBLE");
        puts("");
    }
}

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