poj 2396

       这道题是对有上下界网络流问题的第一次实践。这题不需要求最大流或最下流,只要判断是否存在可行流,如果存在输出这个可行流(根据题目中的这句话判断: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.)。不过话虽这么说,但是不知道OJ能不能真的判断所有可行的情况。

        由于不需要求最大流或者最小流,所以构图然后运行一次最大流算法就好了(具体方法参考:http://blog.csdn.net/u011008379/article/details/38306477)。

        不得不说,这题构图十分麻烦,这主要是和标准输入数据的格式有关,实在要处理太多情况,不过不是很难处理,有耐心就好。

        此外,个人想提出一点对这道题的疑问:

        for(i=a1;i<=a2;i++)
        {
            for(j=b1;j<=b2;j++) 
            {
                if(ch=='=')
                {  
                    //if(up[i][j]<w||down[i][j]>w) return false;       
                    up[i][j]=down[i][j]=w;                             
                }else if(ch=='>') down[i][j]=max(down[i][j],w+1);
                else  if(ch=='<') up[i][j]=min(up[i][j],w-1);                       
            }               
        }
        被注释的这句话是用来判断题目给出的条件是否存在矛盾的,应该没有写错,可是如果把注释去掉,那么就会WA,注释掉才AC,不知为何。如果有哪位大牛发现我的错误,希望告诉我一下,非常感激!

         最后感谢一下这篇文章:POJ2396 题解_forsona&Sona的POJ题解,因为这篇文章里提供了题目数据的链接(http://www.ida.liu.se/projects/progcontest/progsm/2003/),才让我发现这个错误的,真的很感谢。


代码(C++):

#include <cstdlib>
#include <iostream>
#include <queue>

#define MAX_N 30
#define MAX_M 300
#define INF 2000000000
using namespace std;

//#define LOCAL

struct Edge{
    int from;   
    int to;
    int cap;
    int next;   
} edge[MAX_N*MAX_M*4];

int head[MAX_M+MAX_N],up[MAX_M][MAX_N],down[MAX_M][MAX_N],n,m,c,level[MAX_M+MAX_N],sum;

bool get_info()
{
    int count,r,p,w,a1,a2,b1,b2,i,j,k;
    char ch;      
    
    scanf("%d",&count);
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            up[i][j]=INF;
            down[i][j]=0;             
        }             
    } 
    for(k=0;k<count;k++)
    {
        scanf("%d %d %c %d",&r,&p,&ch,&w);
        a1=a2=r,b1=b2=p;
        if(r==0) a1=1,a2=m;
        if(p==0) b1=1,b2=n;
        for(i=a1;i<=a2;i++)
        {
            for(j=b1;j<=b2;j++) 
            {
                if(ch=='=')
                {  
                    //if(up[i][j]<w||down[i][j]>w) return false;       
                    up[i][j]=down[i][j]=w;                             
                }else if(ch=='>') down[i][j]=max(down[i][j],w+1);
                else  if(ch=='<') up[i][j]=min(up[i][j],w-1);                       
            }               
        }               
    } 
    return true;         
}

void add_edge(int u,int v,int cap)
{
     edge[c].from=u;
     edge[c].to=v;
     edge[c].cap=cap;
     edge[c].next=head[u];
     head[u]=c;
     c++;
     
     edge[c].from=v;
     edge[c].to=u;
     edge[c].cap=0;
     edge[c].next=head[v];
     head[v]=c;
     c++;
}

bool build_graph()
{
     int i,j;

     for(i=1;i<=m;i++)
     {
         for(j=1;j<=n;j++)
         {
            if(up[i][j]<down[i][j]) return false;
            add_edge(i,j+200,up[i][j]-down[i][j]); 
                 
            add_edge(i,n+200+3,down[i][j]);
            add_edge(n+200+2,j+200,down[i][j]);  
            
            sum+=down[i][j];          
         }             
     }
     add_edge(n+200+1,0,INF);
     return true;
}

void bfs(int s)
{
     int i,v,u;
     queue<int> qi;
     level[s]=0;
     qi.push(s);
     while(!qi.empty())
     {
         u=qi.front();
         qi.pop();
         for(i=head[u];i!=-1;i=edge[i].next) 
         {
             v=edge[i].to;
             if(edge[i].cap>0&&level[v]==-1)
             {
                 level[v]=level[u]+1;
                 qi.push(v);           
             }                             
         }           
     }
}

int dfs(int u,int t,int f)
{
    int i,v,d,res;
    if(u==t) return f;
    res=0;
    for(i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(level[u]==level[v]-1&&edge[i].cap>0)
        {
            d=dfs(v,t,min(f-res,edge[i].cap));
            if(d>0)
            {
                edge[i].cap-=d;
                edge[i^1].cap+=d;
                res+=d;
                if(res==f) return res;   
            }                                   
        }                              
    }
    return res;
}

int dinic(int s,int t)
{
     int flow,f;
     flow=0;
     while(1)
     {
         memset(level,-1,sizeof(level));    
         bfs(s); 
         if(level[t]==-1) return flow; 
         f=dfs(s,t,INF);
         flow+=f;  
     }
}

int main(int argc, char *argv[])  //超级源点:0,超级汇点:n+200+1,临时源点:n+200+2,临时汇点:n+200+3 
{                                 //expense标号:1~m    site标号:200~n+200 
#ifdef LOCAL
   freopen("in.txt","r",stdin);
   freopen("out.txt","w",stdout);
#endif
    int i,j,t,s,f,u,v;

    scanf("%d",&t); 
    while(t--)
    {
        scanf("%d %d",&m,&n);
        c=0;
        memset(head,-1,sizeof(head));
        sum=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d",&s); 
            sum+=s;           
            add_edge(0,n+200+3,s);
            add_edge(n+200+2,i,s);            
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d",&s);
            sum+=s;
            add_edge(i+200,n+200+3,s);
            add_edge(n+200+2,n+200+1,s);          
        }
        
        if(!get_info()||!build_graph())
        {
            printf("IMPOSSIBLE\n\n");
            continue;              
        } 
        
        f=dinic(n+200+2,n+200+3);
        
        if(f==sum)
        {          
            for(i=(n+m)*4;i<c-2;i+=6)
            {
                 u=edge[i].from;
                 v=edge[i].to;
                 v-=200; 
                 down[u][v]+=edge[i+1].cap;        
            }      
            for(i=1;i<=m;i++)
            {
               printf("%d",down[i][1]);              
               for(j=2;j<=n;j++)
               {
                    printf(" %d",down[i][j]);            
               }
               printf("\n");              
            }       
        }else printf("IMPOSSIBLE\n"); 
        
        if(t>0) printf("\n");  
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

题目( http://poj.org/problem?id=2396):

Budget
Time Limit: 3000MS Memory Limit: 65536K
    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 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值