poj 2396 (有上下界的网络流)

这题题意要注意, 题目中说的 (0,0) 是表示所有的点都要满足,而不是加起来的和满足.

然后根据经典的求有上下界的网络流的建图方法就行了.

首先建立s,t,然后s连上每一个代表行的点,每个代表列的点连上t.  先根据题意求出图中每一条边的上界和下界。 然后再做超级源点和汇点ss,tt. 统计原图中的每一个点的  tmp=所有流入下界和-所有流出下界和, 如果>0 则从ss连一条大小为tmp的边,如果<0 则连一条权值为-tmp的边到tt. 然后从ss到tt求一次最大流,如果结果和ss有关的边都满流则说明存在可行流.

Budget
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 4736   Accepted: 1825   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

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
#define N 250
#define INF 0x3fffffff    

struct node
{
    int b,c,w;
}g[250][250];

int cnt,pre[N];
int n,m;
int s,t;
int ss,tt;
int flag;
int gx[N],gy[N];
int nn;
int lv[N],gap[N];
int save[N];

int sdfs(int k,int w) // SAPb 递归求解可行流
{
    if(k==t) return w;
    int f=0;
    int mi=nn-1;
    for(int i=0;i<=t;i++)
    {
        if(g[k][i].w!=0)
        {
            if(lv[k]==lv[i]+1)
            {
                int tmp=sdfs(i,min(g[k][i].w,w-f));
                f+=tmp;
                g[k][i].w-=tmp;
                g[i][k].w+=tmp;
                if(f==w||lv[s]==nn) return f;
            }
            if(lv[i]<mi) mi=lv[i];
        }
    }
    if(f==0)
    {
        if(--gap[lv[k]]==0)
        {
            lv[s]=nn;
            return f;
        }
        lv[k]=mi+1;
        gap[lv[k]]++;
    }
    return f;
}

int sap(int s1,int t1)
{
    int sum=0;
    nn=t1+1;
    s=s1; t=t1;
    memset(lv,0,sizeof(lv));
    memset(gap,0,sizeof(gap));
    gap[0]=nn;
    while(lv[s]<nn)
    {
        sum+=sdfs(s,INF);
    }
    return sum;
}

int main() 
{
    int t1;
    scanf("%d",&t1);
    while(t1--)
    {
        flag=0;
        scanf("%d%d",&n,&m);
        memset(save,0,sizeof(save));
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++) // 初始化
            for(int j=1;j<=m;j++)
            {
                g[i][n+j].b=0;
                g[i][n+j].c=INF;
                g[i][n+j].w=0;
            }

        int sum1=0,sum2=0;
        int ks=0,kt=n+m+1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&gx[i]);
            g[ks][i].b=gx[i];
            g[ks][i].c=gx[i];
            sum1+=gx[i];
        }

        for(int i=1;i<=m;i++)
        {
            scanf("%d",&gy[i]);
            g[n+i][kt].b=gy[i];
            g[n+i][kt].c=gy[i];
            sum2+=gy[i];
        }

        if(sum1!=sum2) 
        {
            flag=1;
        }
        
        int    tmp;
        scanf("%d",&tmp);
        for(int ii=0;ii<tmp;ii++)
        {
            int x,y,w;
            char c;
            scanf("%d%d %c %d",&x,&y,&c,&w);
            if(x==0&&y==0) // 当有多次循环的时候,注意循环的次序以简洁为主.
            {
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=m;j++)
                    {
                        if(c=='=') g[i][n+j].b=g[i][n+j].c=w;
                        if(c=='>') g[i][n+j].b=max(g[i][n+j].b,w+1); // 这题必须注意的一点,就是当一个点有多个条件时,要取最优条件
                        if(c=='<') g[i][n+j].c=min(g[i][n+j].c,w-1);
                    }
            }
            if(x==0&&y!=0)
            {
                for(int i=1;i<=n;i++)
                {
                    if(c=='=') g[i][n+y].b=g[i][n+y].c=w;
                    if(c=='>') g[i][n+y].b=max(g[i][n+y].b,w+1);
                    if(c=='<') g[i][n+y].c=min(g[i][n+y].c,w-1);
                }
            }
            if(x!=0&&y==0)
            {
                for(int j=1;j<=m;j++)
                {
                    if(c=='=') g[x][n+j].b=g[x][n+j].c=w;
                    if(c=='>') g[x][n+j].b=max(g[x][n+j].b,w+1);
                    if(c=='<') g[x][n+j].c=min(g[x][n+j].c,w-1);
                }
            }
            if(x!=0&&y!=0)
            {
                if(c=='=') g[x][n+y].b=g[x][n+y].c=w;
                if(c=='>') g[x][n+y].b=max(g[x][n+y].b,w+1);
                if(c=='<') g[x][n+y].c=min(g[x][n+y].c,w-1);
            }
        }
        
        //////////////////////////////////////////
        
        int key=0;
        ss=kt+1;
        tt=kt+2;
        for(int i=0;i<=kt;i++) // 建立上界减下界的边,并且统计每个点流入和流出的下界和
            for(int j=0;j<=kt;j++)
            {
                if(g[i][j].b>g[i][j].c)
                {
                    flag=1;
                    break;
                }
                g[i][j].w=g[i][j].c-g[i][j].b;
                save[i]-=g[i][j].b;
                save[j]+=g[i][j].b;
            }

            ////////////////////////

        for(int i=0;i<=kt;i++) // 建立与超级源点和超级汇点的边
        {
            if(save[i]>0)
            {
                g[ss][i].w=save[i];
                key+=save[i];
            }
            else g[i][tt].w=-save[i];
        }    
        
        g[kt][ks].w=INF;
        //////////
        int sum;
        sum=sap(ss,tt); // 求一次是否有可行流
        if(sum!=key)
        {
            printf("IMPOSSIBLE\n");
            continue;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                printf("%d ",g[n+j][i].w+g[i][n+j].b);
            }
            printf("\n");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chenhuan001/p/2932698.html