poj 2396 Budget(有源汇上下界可行流)

Budget
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 5621 Accepted: 2140 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 
 
 
题意:有一个矩阵,n行m列,给出每行元素的和、每列元素的和,并且给出c个约束,每个约束(u, v) = a代表元素(u, v)必须等于a(>或<时以此类推),若u=0,则代表第v列元素必须全部=(或>、<)a。问是否存在一个满足所有约束条件的矩阵,若存在,输出这个矩阵。
思路:设源点s,汇点t,每行和每列看成是一个结点,s到行结点i连一条[sum[i], sum[i]]的边,列节点i到t连一条[sum[i], sum[i]]的边,然后行结点到列节点连满足上下界的边,再虚设一个超级源点ss和超级汇点tt,求一次可行流就可以了。
 
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)
using namespace std;

const int INF = 1e9;
const int maxn = 505;

struct Edge
{
    int u, v, cap, flow, next;
} et[maxn * maxn];
int low[maxn], cnt[maxn], dis[maxn], pre[maxn], cur[maxn], eh[maxn], du[maxn];
int L[maxn][maxn], R[maxn][maxn];
int n, m, s, t, num, ss, tt, sum1, sum2, sum;
void init()
{
    memset(eh, -1, sizeof(eh));
    memset(du, 0, sizeof(du));
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <= m; j++)
        {
            L[i][j] = 0;
            R[i][j] = INF;
        }
    num = sum1 = sum2 = sum = 0;
}
void add(int u, int v, int cap, int flow)
{
    Edge e = {u, v, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap)
{
    add(u, v, cap, 0);
    add(v, u, 0, 0);
}
int isap(int s, int t, int nv)
{
    int u, v, now, flow = 0;
    memset(low, 0, sizeof(low));
    memset(cnt, 0, sizeof(cnt));
    memset(dis, 0, sizeof(dis));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(low[u], et[now].cap - et[now].flow);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
                if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
                    dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
bool judge()
{
    if(sum1 != sum2) return false;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            if(L[i][j] > R[i][j]) return false;
            addedge(i, j + n, R[i][j] - L[i][j]);
            du[i] -= L[i][j];
            du[j + n] += L[i][j];
        }
    return true;
}
void solve()
{
    ss = t + 1, tt = t + 2;
    sum = 0;
    for(int i = 0; i <= t; i++)
    {
        if(du[i] > 0)
        {
            addedge(ss, i, du[i]);
            sum += du[i];
        }
        else if(du[i] < 0) addedge(i, tt, -du[i]);
    }
    addedge(t, s, INF);
    int tmp = isap(ss, tt, tt + 1);
    if(tmp != sum)
    {
        printf("IMPOSSIBLE\n");
        return;
    }
    int cc = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j < m; j++)
        {
            printf("%d ", et[cc].flow + L[i][j]);
            cc += 2;
        }
        printf("%d\n", et[cc].flow + L[i][m]);
        cc += 2;
    }
}
int main()
{
    int ca, a, c, nn, mm;
    char ch[5];
    scanf("%d", &ca);
    while(ca--)
    {
        scanf("%d%d", &n, &m);
        init();
        s = 0, t = n + m + 1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &c);      //行结点[c, c]
            du[s] -= c;
            du[i] += c;
            sum1 += c;
        }
        for(int i = n + 1; i <= n + m; i++)
        {
            scanf("%d", &c);      //列节点[c, c]
            du[i] -= c;
            du[t] += c;
            sum2 += c;
        }
        scanf("%d", &c);
        while(c--)
        {
            scanf("%d%d%s%d", &nn, &mm, ch, &a);
            int r1 = nn, r2 = nn, c1 = mm, c2 = mm;
            if(nn == 0) r1 = 1, r2 = n;
            if(mm == 0) c1 = 1, c2 = m;
            for(int i = r1; i <= r2; i++)
                for(int j = c1; j <= c2; j++)
                {
                    if(ch[0] == '=')
                    {
                        L[i][j] = max(L[i][j], a);
                        R[i][j] = min(R[i][j], a);
                    }
                    else if(ch[0] == '>') L[i][j] = max(L[i][j], a + 1);
                    else R[i][j] = min(R[i][j], a - 1);
                }
        }
        if(judge()) solve();
        else printf("IMPOSSIBLE\n");
        puts("");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值