poj2396 Budget

(http://www.elijahqi.win/2017/12/20/poj2396-budget/)
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

Source
Tehran 2003 Preliminary
1.30AM 把这题样例调试过了qwq 1A了还是蛮开心的 这题相当于是一个有源有汇判断可行流的一道题 题意 是给定 每行每列的和 然后再可能给出每个点的限制 然后就求一个可行解吧 那么我可以把每一行当做一个点 每一列当做一个点 然后从源点向每一行连上下界都是行的和的点 然后 每个点相当于连接了每个行和列之间的关系 所以如果每个点有限制那就 去把这个点所连接起的行和列中间建立有上下界的边 就体现在d数组上 就是这个点的入度减出度 因为有源有汇所以 我还需要再开一个源和汇 我在原来的源和汇之间从汇连向源一个inf的边 表示 我汇到源还能流回来 然后上下界的网络流就是要是这个点 入度是小于出度的那我相当于为了满足平衡我的平衡流量流入的会多 所以我需要从这个点向我新设的源点连接 反之我应该从这个点连到新设的汇点 跑一遍最大流 那么每个边的可行解就都存在了他们的反向边上 再加上下界输出即可 要是最大流≠我需要平衡的流量 那impossible


#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 4400
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x*f;
}
struct node{
    int x,y,z,next;
}data[320000];
int num=1,h[N],level[N],low[220][20],up[220][20],T,TT,m,n,d[N],rs,rt;
inline void insert1(int x,int y,int z){
    data[++num].y=y;data[num].next=h[x];data[num].z=z;h[x]=num;data[num].x=x;
    data[++num].y=x;data[num].next=h[y];data[num].z=0;h[y]=num;data[num].x=y;
}
inline bool bfs(){
    memset(level,0,sizeof(level));queue<int>q;level[rs]=1;q.push(rs);
    while(!q.empty()){
        int x=q.front();q.pop();
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z;if (level[y]||!z) continue;
            level[y]=level[x]+1;q.push(y);if (y==rt) return 1;
        }
    }return 0;
}
inline int dfs(int x,int s){
    if (x==rt) return s;int ss=s;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y,z=data[i].z;
        if (level[x]+1==level[y]&&z){
            int xx=dfs(y,min(z,s));if(!xx) level[y]=0;
            s-=xx;data[i].z-=xx;data[i^1].z+=xx;if(!s) return ss;
        }
    }return ss-s;
}
int main(){
    freopen("poj2396.in","r",stdin);
    TT=read();
    while(TT--){
        m=read();n=read();T=n+m+1;rs=T+1;rt=T+2;num=1;memset(h,0,sizeof(h));memset(d,0,sizeof(d));insert1(T,0,inf);bool flag=0;
        for (int i=1;i<=m;++i){int aa=read();if (aa<0) flag=1;d[i]+=aa;d[0]-=aa;}
        for (int i=1;i<=n;++i){int aa=read();if (aa<0) flag=1;d[i+m]-=aa;d[T]+=aa;}
        for (int i=1;i<=m;++i) for (int j=1;j<=n;++j) low[i][j]=0,up[i][j]=inf;
    //  for (int i=2;i<=num;++i) printf("%d %d %d\n",data[i].x,data[i].y,data[i].z);
        int k=read();while(k--){
            int a=read(),b=read();char ch=gc();while(ch!='<'&&ch!='>'&&ch!='=') ch=gc();int v=read();
            if (ch=='<') v--;else if (ch=='>') v++;
            if (!a&&!b){
                for (int i=1;i<=m;++i)
                    for (int j=1;j<=n;++j)
                        if (ch=='=') low[i][j]=up[i][j]=v;else if(ch=='<') up[i][j]=v;else low[i][j]=v;
                continue;
            }if (!a){
                for (int i=1;i<=m;++i)
                    if (ch=='=') low[i][b]=up[i][b]=v;else if(ch=='<') up[i][b]=v;else low[i][b]=v;continue;
            }if(!b){
                for (int i=1;i<=n;++i)
                    if (ch=='=') low[a][i]=up[a][i]=v;else if(ch=='<') up[a][i]=v;else low[a][i]=v;continue;
            }if (ch=='=') low[a][b]=up[a][b]=v;else if(ch=='<') up[a][b]=v;else low[a][b]=v;
        }
        for (int i=1;i<=m;++i)
            for (int j=1;j<=n;++j)
                insert1(i,j+m,up[i][j]-low[i][j]),d[i]-=low[i][j],d[j+m]+=low[i][j];
        int sum=0,ans=0;
    //  for (int i=0;i<=n+m+1;++i) printf("%d ",d[i]);printf("\n");
        for (int i=0;i<=n+m+1;++i) if (d[i]<0) insert1(i,rt,-d[i]);else insert1(rs,i,d[i]),sum+=d[i];
        while(bfs()) ans+=dfs(rs,inf);
        if(ans!=sum||flag){printf("IMPOSSIBLE\n");if(TT) printf("\n");continue;}
    //  for (int i=2;i<=num;++i) printf("%d %d %d\n",data[i].x,data[i].y,data[i].z);
        for (int i=m+1;i<=m+n;++i){
            for (int j=h[i];j;j=data[j].next){
                if (j&1==0) continue;int y=data[j].y;low[y][i-m]+=data[j].z;
            }
        }
        for (int i=1;i<=m;++i){
            for (int j=1;j<=n;++j) printf("%d ",low[i][j]);printf("\n");
        }if(TT) printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值