1. 程式人生 > >POJ 2396 Budget 上下界有源匯可行流

POJ 2396 Budget 上下界有源匯可行流

                                                                                                        Budget

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 8851 Accepted: 3284 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 

題解:

上下界有源匯可行流

新增 t ----INF---> s  是流量平衡

新增超級源點匯點ss,tt

其他同無源匯可行流建圖  點選進入

注意wrong點  strictly greater than  < > 建圖的時候注意 減1加1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 300+10;
const int maxm = 1e6+100;
const int INF = 0x3f3f3f3f;

int n,m,tot;

int l[maxn],h[maxn],cur[maxn];
struct edge
{
  int to;
  int c;
  int next;
  edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
 }es[maxm*2];//記錄邊 注意是2倍

void add_edge(int u, int v, int c)
{
    es[tot] = edge(v,c,h[u]);
    h[u] = tot++;
    es[tot] = edge(u,0,h[v]);
    h[v] = tot++;
}

bool bfs(int s, int t)
{
   memset(l,0,sizeof(l));
   l[s] = 1;
   queue <int> q;
   q.push(s);
   while(!q.empty())
   {
    int u = q.front();
    q.pop();
    if(u == t)  return true;
    for(int i = h[u]; i != -1; i = es[i].next)
        {
         int v = es[i].to;
         if(!l[v] && es[i].c) {l[v] = l[u] + 1; q.push(v);}
        }
   }
   return false;
}

int dfs(int x, int t, int mf)
{
    if(x == t) return mf;
    int ret = 0;
    for(int &i = cur[x]; i != -1; i = es[i].next)
    {
      if(es[i].c && l[x] == l[es[i].to] - 1)
      {
        int f = dfs(es[i].to,t,min(es[i].c,mf - ret));
        es[i].c -= f;
        es[i^1].c += f;
        ret += f;
        if(ret == mf) return ret;
      }
    }
    return ret;
}

int dinic(int s, int t)
{
  int ans = 0;
  while(bfs(s,t))
  {
   for(int i = 0; i <= t; i++) cur[i] = h[i];
   ans += dfs(s,t,INF);
   }
  return ans;
}


int B[maxn][30];
int C[maxn][30];
int in[maxn];

int main()
{
 //freopen("in.txt","r",stdin);
 int T;
 scanf("%d",&T);
 while(T--)
 {
   scanf("%d%d",&n,&m);
   int s = n+m+1, t = n+m+2,ss = 0,tt=t+1;
   tot = 0;
   memset(h,-1,sizeof(h));
   memset(in,0,sizeof(in));
   for(int i = 0; i < maxn; i++)
     for(int j = 0; j < 30; j++)
             { C[i][j] = 1000; B[i][j] = -1000;}
   int p;
   for(int i = 1; i <= n; i++)
     {scanf("%d",&p); in[i] += p; in[s] -= p;}
   for(int i = 1; i <= m; i++)
     {scanf("%d",&p); in[i+n] -= p; in[t] += p;}

  int c;
  scanf("%d",&c);
  while(c--)
  {
     int r,q,v;
     char ch;
     scanf("%d%d",&r,&q);
     scanf(" %c",&ch);     
     scanf("%d",&v);
   
   if(r == 0)
     {
         if(q == 0)
             for(int i = 1; i <= n; i++)
                for(int j = 1; j <= m; j++)
                    if(ch == '=')  B[i][j] = C[i][j] = v; 
                    else if(ch == '<') C[i][j] = min(C[i][j],v-1);
                    else B[i][j] = max(B[i][j],v+1);
         else for(int i = 1; i <= n; i++)
                    if(ch == '=')  B[i][q] = C[i][q] = v; 
                    else if(ch == '<') C[i][q] = min(C[i][q],v-1);
                    else B[i][q] = max(B[i][q],v+1);
     }
  else 
    {
        if(q == 0)
          for(int j = 1; j <= m; j++)
	  if(ch == '=') B[r][j] = C[r][j] = v;
	  else if(ch == '<') C[r][j] = min(C[r][j],v-1);
	  else B[r][j] = max(B[r][j],v+1);
        else 
           if(ch == '=')  B[r][q] = C[r][q] = v; 
           else if(ch == '<') C[r][q] = min(C[r][q],v-1);
           else B[r][q] = max(B[r][q],v+1);
    }
  }

 
  for(int i = 1; i <= n; i++)
     for(int j = 1; j <= m; j++)
     {
	     in[i] -= B[i][j];
	     in[j+n] += B[i][j];
	     add_edge(i,j+n,C[i][j]-B[i][j]);
     }
  
  add_edge(t,s,INF); 
 int res = 0;  
 for(int i = 1; i <= t; i++)
   {
   if(in[i] > 0)  {add_edge(ss,i,in[i]);res += in[i];}
   if(in[i] < 0)  add_edge(i,tt,-in[i]);
   }

  int ans = dinic(ss,tt);
  if(ans != res) printf("IMPOSSIBLE\n");
  else {
         int k = 1;
	 for(int i = 1; i <= n; i++)
	   for(int j = 1; j <= m; j++,k+=2)
		 printf("%d%c",B[i][j]+es[k].c, j == m? '\n':' ');
    }
 printf("\n");
 }
 return 0;
}