POJ 2396 Budget 题解【网络流】【上下界】

作者: wjyyy 分类: 上下界网络流,网络流,解题报告 发布时间: 2019-01-19 09:46

点击量:392

这是一道有源汇点的上下界可行流问题。

Description

We are supposed to make a budget proposal for this multi-site competition. The budget proposal is a matrix where the rows represent different kinds of expenses and the columns represent different sites. We had a meeting about this, some time ago where we discussed the sums over different kinds of expenses and sums over different sites. There was also some talk about special constraints: someone mentioned that Computer Center would need at least 2000K Rials for food and someone from Sharif Authorities argued they wouldn’t use more than 30000K Rials for T-shirts. Anyway, we are sure there was more; we will go and try to find some notes from that meeting.

And, by the way, no one really reads budget proposals anyway, so we’ll just have to make sure that it sums up properly and meets all constraints.

Input

The first line of the input contains an integer N, giving the number of test cases. The next line is empty, then, test cases follow: The first line of each test case contains two integers, m and n, giving the number of rows and columns (m <= 200, n <= 20). The second line contains m integers, giving the row sums of the matrix. The third line contains n integers, giving the column sums of the matrix. The fourth line contains an integer c (c < 1000) giving the number of constraints. The next c lines contain the constraints. There is an empty line after each test case.

Each constraint consists of two integers r and q, specifying some entry (or entries) in the matrix (the upper left corner is 1 1 and 0 is interpreted as “ALL”, i.e. 4 0 means all entries on the fourth row and 0 0 means the entire matrix), one element from the set {<, =, >} and one integer v, with the obvious interpretation. For instance, the constraint 1 2 > 5 means that the cell in the 1st row and 2nd column must have an entry strictly greater than 5, and the constraint 4 0 = 3 means that all elements in the fourth row should be equal to 3.
Output

For each case output a matrix of non-negative integers meeting the above constraints or the string “IMPOSSIBLE” if no legal solution exists. Put one empty line between matrices.

Sample Input

2

2 3 
8 10 
5 6 7 
4 
0 2 > 2 
2 1 = 3 
2 3 > 2 
2 3 < 5 

2 2 
4 5 
6 7 
1 
1 1 > 10

Sample Output

2 3 3 
3 3 4 

IMPOSSIBLE 

题意:

现有一个矩阵,已知它每行的和、每列的和。并且每个位置上的值都有一个约束,以x y op z表示第$ x$行第$ y$列的值应该op(>,<,=)$ z$。

如果存在这样的矩阵,输出任意一组可行解;否则输出IMPOSSIBLE。每组数据之间输出一个空行。

题解:

有源汇的有上下界的网络流。

控制上下界和sgu 194 Reactor Cooling 题解这题一样,需要对原始源点汇点进行改造。

我们知道在有上下界的网络流中,超级源点$ S,T$起到了驱动的作用。

而源点汇点流出的边由于必须满流,也可以被驱动。因此只需要从$ S$连接一条附加边到原始源点$ s$,容量为源点流入流量即可。

当所有附加边(包括控制上下界的)都满流时,则原图有可行解。

在这个题中,每个点要被拆成两个点来控制这个点的权值上下界。然后每一行都有一个源点可以流出,每一列都有一个汇点可以流入。根据这些限制来添加源点汇点和附加边,这个题就完成了。

Code:

#include<cstdio>
#include<cstring>
#define inf 0x3fffffff
int Min(int x,int y)
{
    return x<y?x:y;
}
int Max(int x,int y)
{
    return x>y?x:y;
}
struct edge
{
    int n,nxt,v,ori;
    edge(int n,int nxt,int v,int ori)
    {
        this->n=n;
        this->nxt=nxt;
        this->v=v;
        this->ori=ori;
    }
    edge(){}
}e[50000];//4000个点 每个点需要一对边、每个点可能连到源点汇点 24000
int head[9000],ecnt=-1;
void add(int from,int to,int v,int ori)
{
    e[++ecnt]=edge(to,head[from],v,ori);
    head[from]=ecnt;
    e[++ecnt]=edge(from,head[to],0,0);
    head[to]=ecnt;
}
int up[9000],down[9000];
//[1,n*m],[n*m+1,2*n*m]表示原来的点
//[8000+1,8000+n]表示行源,[8000+n+1,8000+n+m]表示列汇
int d[9000],q[9000];
bool bfs()
{
    int l=0,r=0;
    memset(d,0,sizeof(d));
    d[8888]=1;
    q[++r]=8888;
    while(l<r)
    {
        int x=q[++l];
        for(int i=head[x];~i;i=e[i].nxt)
            if(e[i^1].v&&!d[e[i].n])
            {
                d[e[i].n]=d[x]+1;
                q[++r]=e[i].n;
            }
    }
    return d[0]>0;
}
int dinic(int x,int in)
{
    if(x==8888)
        return in;
    int flow=in;
    for(int i=head[x];flow&&~i;i=e[i].nxt)
        if(e[i].v&&d[e[i].n]==d[x]-1)
        {
            int tmp=dinic(e[i].n,Min(e[i].v,flow));
            if(!tmp)
                d[e[i].n]=0;
            e[i].v-=tmp;
            e[i^1].v+=tmp;
            flow-=tmp;
        }
    return in-flow;
}
char op[100];
int ans[9000];
int main()
{
#ifdef wjyyy
    freopen("data.in","r",stdin);
    freopen("ans.out","w",stdout);
#endif
    int T,n,m,q;
    scanf("%d",&T);
    while(T--)
    {
        int u,v,w;
        memset(up,0x3f,sizeof(up));
        memset(down,0,sizeof(down));
        memset(head,-1,sizeof(head));
        ecnt=-1;
        scanf("%d%d",&n,&m);
        int sum=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&u);
            add(0,8000+i,u,0);
            sum+=u;
            for(int j=1;j<=m;++j)
                add(8000+i,(i-1)*m+j,inf,0);//连到的是各点的入边
        }
        for(int i=1;i<=m;++i)
        {
            scanf("%d",&u);
            add(8000+n+i,8888,u,0);
            for(int j=1;j<=n;++j)
                add(n*m+(j-1)*m+i,8000+n+i,inf,0);//连到的是各点的出边
        }
        scanf("%d",&q);
        for(int i=1;i<=q;++i)
        {
            scanf("%d%d",&u,&v);
            scanf("%s",op);
            scanf("%d",&w);
            if(u==0)
            {
                for(int j=1;j<=n;++j)
                    if(v==0)
                        for(int k=1;k<=m;++k)
                        {
                            if(op[0]!='<')
                                down[(j-1)*m+k]=Max(down[(j-1)*m+k],w+(op[0]=='>'));
                            if(op[0]!='>')
                                up[(j-1)*m+k]=Min(up[(j-1)*m+k],w-(op[0]=='<'));
                        }
                    else
                    {
                        if(op[0]!='<')
                            down[(j-1)*m+v]=Max(down[(j-1)*m+v],w+(op[0]=='>'));
                        if(op[0]!='>')
                            up[(j-1)*m+v]=Min(up[(j-1)*m+v],w-(op[0]=='<'));
                    }
            }
            else
            {
                if(v==0)
                    for(int k=1;k<=m;++k)
                    {
                        if(op[0]!='<')
                            down[(u-1)*m+k]=Max(down[(u-1)*m+k],w+(op[0]=='>'));
                        if(op[0]!='>')
                            up[(u-1)*m+k]=Min(up[(u-1)*m+k],w-(op[0]=='<'));
                    }
                else
                {
                    if(op[0]!='<')
                        down[(u-1)*m+v]=Max(down[(u-1)*m+v],w+(op[0]=='>'));
                    if(op[0]!='>')
                        up[(u-1)*m+v]=Min(up[(u-1)*m+v],w-(op[0]=='<'));
                }
            }
        }
        bool flag=0;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j)
            {
                int pos=(i-1)*m+j;
                if(up[pos]<down[pos])
                    flag=1;
                add(pos,n*m+pos,up[pos]-down[pos],up[pos]);
                ans[pos]=ecnt-1;
                add(0,n*m+pos,down[pos],0);
                add(pos,8888,down[pos],0);
                sum+=down[pos];
            }
        while(bfs())
        {
            int tmp;
            do
            {
                tmp=dinic(0,inf);
                sum-=tmp;
            }while(tmp);
        }
        if(flag||sum!=0)
        {
            puts("IMPOSSIBLE\n");
            continue;
        }
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=m;++j)
                printf("%d ",e[ans[(i-1)*m+j]].ori-e[ans[(i-1)*m+j]].v);
            puts("");
        }
        puts("");
    }
    return 0;
}

2
说点什么

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors
  Subscribe  
最新 最旧 得票最多
提醒
trackback

… [Trackback]

[…] Read More Information here to that Topic: wjyyy.top/3087.html […]

trackback

… [Trackback]

[…] Find More Information here on that Topic: wjyyy.top/3087.html […]

/* */