POJ2396 Budget 有上下界的最大流问题

10 篇文章 0 订阅
7 篇文章 0 订阅
Budget
Time Limit: 3000MS      Memory Limit: 65536K
Total Submissions: 7531     Accepted: 2828      Special Judge

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

这题建图很麻烦…超麻烦…..

以每行每列分别作为一个点
添加s到每行一条边 上下界为行和
添加每列到t一条边 上下界是列和
i行到j列的上下界就等于i行j列的数的限制

明显s出发的边 拆去必要边后 流量都为0
确认tflow=最大流后 再怎么跑流量都不会增加
所以跑一遍最大流即可

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 200 + 5;
const int M = 20 + 5;
const int E = N*M*10 + 2*(N + M);
const int V = N + M + 10;

int level[V];//标号 level[i]:s到i的最短距离

struct Edge{
    int to,c,next;
}edge[E];
int head[V];

int s,t,s_,t_;

inline void add_edge(int k,int u,int v,int c){
    edge[k].to = v;
    edge[k].c = c;
    edge[k].next = head[u];
    head[u] = k;
}
int edge_count=0;
inline void add_e(int u,int v,int c){
    add_edge(edge_count++,u,v,c);
    add_edge(edge_count++,v,u,0);
}
inline void add_e(int u,int v,int low,int up){
    add_e(u,v,up-low);
    add_e(s_,v,low);
    add_e(u,t_,low);
}

bool bfs(int s,int t,int n){//标号 计算level
    deque<int>que;
    fill(level,level+n+1,-1);
    que.push_back(s);
    level[s] = 0;
    while(!que.empty()){
        int u = que.front();
        if(u == t){
            return true;
        }
        que.pop_front();
        for(int i = head[u];i!=-1;i = edge[i].next){
            if(edge[i].c > 0 && level[edge[i].to] == -1){
                level[edge[i].to] = level[u] + 1;
                que.push_back(edge[i].to);
            }
        }
    }
    return false;
}

int dfs(int u,int t,int maxf){//u:所在的点 t:汇点 maxf:能流到u的流量
    if(u == t){
        return maxf;
    }
    int sum = 0;
    for(int i = head[u];i!=-1;i = edge[i].next){
        Edge&e = edge[i];
        if(e.c>0 && level[e.to]>level[u]){
            int f = dfs(e.to,t,min(maxf - sum,e.c));
            sum += f;
            edge[i].c -= f;
            edge[i^1].c += f;
            if(sum == maxf){//流量用完了
                break;
            }
        }
    }
    level[u]=-1;
    return sum;
}

int dinic(int s,int t,int n){//s:源点 t:汇点 n:点数
    int ans = 0;
    while(bfs(s,t,n)){
        ans += dfs(s,t,2*INF);
    }
    return ans;
}

/*
 * 1-n:行
 * n+1-n+m:列
 * n+m+1:S
 * n+m+2:T
 * n+m+3:S'
 * n+m+4:T'
 * */

int constraint[N][M][2];//0:low 1:up
bool add_ct(int a,int b,char ch,int c){//a行b列 ch c 约束
    int&low=constraint[a][b][0],&up=constraint[a][b][1];
    switch(ch){
    case'=':
        if(low<=c&&c<=up){
            low=up=c;
            return true;
        }
        break;
    case'<':
        if(low<=c-1){
            up=min(up,c-1);
            return true;
        }
        break;
    case'>':
        if(up>=c+1){
            low=max(low,c+1);
            return true;
        }
        break;
    }
    return false;
}

bool make_constrain(int n,int m){//计算约束
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            constraint[i][j][0]=0;
            constraint[i][j][1]=INF;
        }
    }
    int num=0;
    scanf("%d",&num);
    bool ans=true;
    while(num--){
        int a,b,c;
        char ch;
        scanf("%d%d%*c%c%d",&a,&b,&ch,&c);
        int l1,l2,l3,l4;
        if(a==0){
            l1=1,l2=n;
        }
        else{
            l1=l2=a;
        }
        if(b==0){
            l3=1,l4=m;
        }
        else{
            l3=l4=b;
        }
        for(int i=l1;i<=l2;++i){
            for(int j=l3;j<=l4;++j){
                ans &= add_ct(i,j,ch,c);
            }
        }
    }
    return ans;
}

void slove(){
    int n, m;
    int tflow=0;//总必要流量
    scanf("%d%d",&n,&m);
    fill(head,head+n+m+4+1,-1);
    s=n+m+1,t=s+1,s_=t+1,t_=s_+1;
    edge_count=0;
    //加边
    for(int i=1;i<=n;++i){//s到行
        int x;
        scanf("%d",&x);
        add_e(s,i,x,x);
        tflow += x;
    }
    for(int i=1;i<=m;++i){//列到t
        int x;
        scanf("%d",&x);
        add_e(n+i,t,x,x);
        tflow += x;
    }

    if(!make_constrain(n,m)){//约束矩阵
        puts("IMPOSSIBLE");
        return;
    }
    for(int i=1;i<=n;++i){//添加表示每个位置的约束的边
        for(int j=1;j<=m;++j){
            int*k=constraint[i][j];
            add_e(i,n+j,k[0],k[1]);
            tflow += k[0];
        }
    }
    add_e(t,s,0,INF);//t到s 保持流量守恒
    int f=dinic(s_,t_,n+m+4);
    if(f!=tflow){
        puts("IMPOSSIBLE");
    }
    else{
        for(int i=1;i<=n;++i){
            for(int j=head[i];j!=-1;j=edge[j].next){
                Edge&e=edge[j];
                if(1+n<=e.to&&e.to<=n+m){
                    constraint[i][e.to-n][0] += edge[j^1].c;//必要f+反向边中f=总f
                }
            }
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                printf("%d ",constraint[i][j][0]);
            }
            putchar('\n');
        }
    }
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        slove();
        if(T){
            putchar('\n');
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值