1. 程式人生 > >算法復習——有源匯上下界可行流(bzoj2396)

算法復習——有源匯上下界可行流(bzoj2396)

取消 meeting greate algorithm ctype main names traints n)

題目:

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

題解:

算是求有源匯上下界最大流的一道模板題···只不過建圖有點麻煩····

先說有源匯上下界最大流最小流的基本求法(引用自農民伯伯-Coding):

有源匯網絡的可行流

對於流量有上下界的有源匯網絡,原圖中存在源點S和匯點T,為了求可行流,先將其轉換為無源匯網絡。

從T-->S引入一條邊,其流量上下界為[0, INF],此時原圖變為了無源匯網絡,然後按照無源匯網絡求解可行流的方法來求解。
技術分享

技術分享

有源匯網絡的最大流

要求最大流,先求可行流,通過“有源匯網絡的可行流”的求解方法來判斷有源匯網絡存在可行流。
若存在可行流,記從S流出的流量sum1,然後將T-->S的邊取消,再次從S到T求解網絡的最大流,記從S流出的流量sum2. 那麽該有源匯網絡的最大流為 sum1 + sum2.
其中,sum1是在網絡滿足流量下界的條件下,從源點S流出的流量;求出sum1之後,網絡中可能還有余量可以繼續增廣,那麽再次求解從S到T的最大流,得到sum2,sum1 + sum2即為最終的最大流。

有源匯網絡的最小流

求解有源匯網絡最小流分為以下幾步:
(1)對SS到TT求一次最大流,即為f1.(在有源匯的情況下,先把整個網絡趨向必須邊盡量滿足的情況);
(2)添加一條邊T-->S,流量上限為INF,這條邊即為P.(構造無源網絡)
(3)對SS到TT再次求最大流,即為f2。(判斷此時的網絡中存在可行流,同時求出SS-->TT最大流)
如果所有必須邊都滿流,證明存在可行解,原圖的最小流為“流經邊P的流量”(原圖已經構造成無源匯網絡,對於S同樣滿足 入流和==出流和,只有新添加的邊流向S,而S的出流就是原圖的最小流)。
註: 最小流求法的正確性不知如何證明,待繼續學習。

代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=1000;
const int M=1000005;
const int inf=0x3f3f3f3f;
int tot=1,first[N],next[M],go[M],rest[M],lev[N],cur[N];
int tr[N],tc[N],n,m,low[N][N],up[N][N],T,src,des,ss,tt,sum1,sum2,k,ans,id[N][N],p;
bool jud[N][N];
inline void comb(int a,int b,int c)
{
  next[++tot]=first[a],first[a]=tot,go[tot]=b,rest[tot]=c;
  next[++tot]=first[b],first[b]=tot,go[tot]=a,rest[tot]=0;
}
inline void comb1(int a,int b,int c)
{ 
  next[tot]=first[a],first[a]=tot,go[tot]=b,rest[tot]=c;
  next[++tot]=first[b],first[b]=tot,go[tot]=a,rest[tot]=0;
}
inline int R()
{
  char c;
  int f=0;
  for(c=getchar();c<0||c>9;c=getchar());
  for(;c<=9&&c>=0;c=getchar())
    f=(f<<3)+(f<<1)+c-0;
  return f;
}
inline void pre()
{
  memset(first,0,sizeof(first));
  memset(low,0,sizeof(low));
  memset(up,inf,sizeof(up));
  memset(tr,0,sizeof(tr));
  memset(tc,0,sizeof(tc));
  tot=1,src=0,des=n+m+1,ss=n+m+2,tt=n+m+3,sum1=0,sum2=0,ans=0;
}
inline bool bfs()
{
  for(int i=src;i<=tt;i++)  cur[i]=first[i],lev[i]=-1;
  static int que[N],tail,u,v;
  que[tail=1]=ss;
  lev[ss]=0;
  for(int head=1;head<=tail;head++)
  {
    u=que[head];
    for(int e=first[u];e;e=next[e])
    {
      if(lev[v=go[e]]==-1&&rest[e])
      {
        lev[v]=lev[u]+1;
        que[++tail]=v;
        if(v==tt)  return true;
      }
    }
  }
  return false;
}
inline int dinic(int u,int flow)
{
  if(u==tt)
    return flow;
  int res=0,delta,v;
  for(int &e=cur[u];e;e=next[e])
  {
    if(lev[v=go[e]]>lev[u]&&rest[e])
    {
      delta=dinic(v,min(flow-res,rest[e]));
      if(delta)
      {
        rest[e]-=delta;
        rest[e^1]+=delta;
        res+=delta;
        if(res==flow)  break;
      }
    }
  }
  if(flow!=res)  lev[u]=-1;
  return res;
}
inline void maxflow()
{
  while(bfs())
    ans+=dinic(ss,inf);
}
int main()
{
  //freopen("a.in","r",stdin);
  T=R();
  int a,b,c;
  char s[5];
  while(T--)
  {
    scanf("\n");
    n=R(),m=R();
    pre();
    for(int i=1;i<=n;i++)
    {
      a=R();
      tc[src]+=a,tr[i]+=a;  
      sum1+=a;
    }
    for(int i=1;i<=m;i++)
    {
      a=R();
      tr[des]+=a,tc[n+i]+=a;
      sum2+=a;
    }
    if(sum1!=sum2)  
    {  
      cout<<"IMPOSSIBLE"<<endl;
      cout<<endl;
      continue;
    }
    k=R();    
    while(k--)
    {
      scanf("%d%d%s%d",&a,&b,s,&c);
      if(a==0&&b==0)
      {
        for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
          {
            if(s[0]===)
            {  
              low[i][j]=max(low[i][j],c);
              up[i][j]=min(up[i][j],c);
            }
            if(s[0]==<)  
              up[i][j]=min(up[i][j],c-1);
            if(s[0]==>)
              low[i][j]=max(low[i][j],c+1);
          }
      }
      else if(a==0)
        for(int i=1;i<=n;i++)
        {
          if(s[0]===)
          {
            low[i][b]=max(low[i][b],c);
            up[i][b]=min(up[i][b],c);
          }
          if(s[0]==<)
            up[i][b]=min(up[i][b],c-1);
          if(s[0]==>)
            low[i][b]=max(low[i][b],c+1);
        }     
      else if(b==0)
        for(int i=1;i<=m;i++)
        {
          if(s[0]===)
          {
            low[a][i]=max(low[a][i],c);
            up[a][i]=min(up[a][i],c);
          }
          if(s[0]==<)
            up[a][i]=min(up[a][i],c-1);
          if(s[0]==>)
            low[a][i]=max(low[a][i],c+1);
        }
      else
      {
        if(s[0]===)
          {
            low[a][b]=max(low[a][b],c);
            up[a][b]=min(up[a][b],c);
          }
        if(s[0]==<)
          up[a][b]=min(up[a][b],c-1);
        if(s[0]==>)
          low[a][b]=max(low[a][b],c+1); 
      }
    }
    bool flag=false;
    for(int i=1;i<=n;i++)
    {  
      if(flag==true)  break;
      for(int j=1;j<=m;j++)
      {
        if(low[i][j]>up[i][j]) 
        { 
          flag=true;
          break;
        }
        else
        {
          tr[n+j]+=low[i][j];
          tc[i]+=low[i][j];
          id[i][j]=++tot;
          comb1(i,n+j,up[i][j]-low[i][j]);
        }
      }
    }
    if(flag==true)
    {
      cout<<"IMPOSSIBLE"<<endl;
      cout<<endl;
      continue;
    }
    int cnt=0;
    p=++tot;
    comb1(des,src,inf);
    for(int i=src;i<=des;i++)
    {
      if(tr[i]>tc[i])
      {  
        comb(ss,i,tr[i]-tc[i]);
        cnt+=tr[i]-tc[i];
      }
      if(tc[i]>tr[i])
        comb(i,tt,tc[i]-tr[i]);
    }
    maxflow();
    if(ans!=cnt)
    {
      cout<<"IMPOSSIBLE"<<endl;
      cout<<endl;
      continue;
    }
    rest[p]=rest[p^1]=0;
    maxflow();  
    for(int i=1;i<=n;i++)
    {  
      for(int j=1;j<=m;j++)
        cout<<rest[id[i][j]^1]+low[i][j]<<" ";
      cout<<endl;
    }
    cout<<endl;
  }
  return 0;
}

算法復習——有源匯上下界可行流(bzoj2396)