POJ2396 Budget

POJ2396 Budget

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
Source
Tehran 2003 Preliminary

題目大意:給一個n*m的格子填數,有行列和單個格子的限制。求是否有可行解並輸出一組可行的填法。

解:
這道題是有上下界的網絡流。

建圖:設置一個source和一個sink點,將sourse與行標號1~m連接,流量限制爲行的數的和;將列標號1~n與sink連接,流量限制爲列的數的和;依次將對應的行列標號相連,設爲a行與b行相連,代表原圖中(a,b)的格子,所以流量限制爲格子的數的範圍。

要求解這個有上下界限制的網絡流的一個可行解,且是有源匯的。轉化爲一個沒有源匯的上下界網絡流處理,即連一條從sink到source的容量爲inf的邊。

重新構圖:設置一個superSource和一個superSink,superSource負責提供每一個點的最低流量,superSink負責接收每一個點的最低流量,原來的邊變爲只有容量上限的邊,轉化爲普通網絡流處理,跑一遍dinic即可。

注意:用long long

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 600;
const int MAXM = 1000000;
const ll INF = 1e15;

struct Edge{
    ll t, f, nxt;
};

struct MCMF{
    ll src, sink, tot;
    ll vis[MAXN], dis[MAXN], g[MAXN];
    Edge e[1000000];
    queue<int> q;

    void init(){
        tot = 1;
        memset(g, 0, sizeof g);
    }

    void addEdge(ll u, ll v, ll c){
        e[++tot].t = v;
        e[tot].f = c;
        e[tot].nxt = g[u];
        g[u] = tot;
        e[++tot].t = u;
        e[tot].f = 0;
        e[tot].nxt = g[v];
        g[v] = tot;
    }

    void bfs(){
        memset(dis, 0, sizeof dis);
        q.push(src);
        vis[src] = 1;
        while (!q.empty()) {
          int x = q.front();
          q.pop();
          for (int i = g[x]; i; i = e[i].nxt)
            if (e[i].f && !vis[e[i].t]) {
              vis[e[i].t] = 1;
              dis[e[i].t] = dis[x] + 1;
              q.push(e[i].t);
            }
        }
    }

    ll dfs(int x, ll delta) {
        if (x == sink) return delta;
        ll res = 0;
        for (int i = g[x]; delta && i; i = e[i].nxt)
          if (e[i].f && dis[e[i].t] == dis[x] + 1) {
            int dd = dfs(e[i].t, min(e[i].f, delta));
            e[i].f -= dd;
            e[i^1].f += dd;
            delta -= dd;
            res += dd;
          }
        return res;
    }

    ll maxFlow(){
        ll res = 0;
        while (true) {
          memset(vis, 0, sizeof vis);
          bfs();
          if (!vis[sink]) return res;
          res += dfs(src, INF);
        }
    }
}MF;

ll n, m;
ll _up[MAXN][MAXN], _low[MAXN][MAXN], ans[MAXN][MAXN], row[MAXN], column[MAXN];
ll up[MAXM], low[MAXM], u[MAXM], v[MAXM], tot_in[MAXM], tot_out[MAXM];

bool lowbound_flow(ll src, ll sink, ll tot) {
    MF.init();
    memset(tot_in, 0, sizeof tot_in);
    memset(tot_out, 0, sizeof tot_out);
    memset(ans, 0, sizeof ans);
    for (int i = 1; i <= tot; ++i) {
      if (up[i] < low[i]) return 0;
      tot_in[v[i]] += low[i];
      tot_out[u[i]] += low[i];
      MF.addEdge(u[i], v[i], up[i] - low[i]);
    }
    MF.addEdge(sink, src, INF);
    int superSrc = sink + 1;
    int superSink = sink + 2;
    MF.src = superSrc;
    MF.sink = superSink;
    for (int i = 0; i <= n+m+1; ++i) {
      if (tot_in[i]) MF.addEdge(superSrc, i, tot_in[i]);
      if (tot_out[i]) MF.addEdge(i, superSink, tot_out[i]);
    }
    MF.maxFlow();
    for (int i = MF.g[superSrc]; i; i = MF.e[i].nxt)
      if (MF.e[i].f && MF.e[i].t != sink)
        return 0;
    for (int j = 1; j <= m; ++j)
      for (int i = MF.g[j]; i; i = MF.e[i].nxt)
        if (MF.e[i].t > m && MF.e[i].t <= n + m)
          ans[j][MF.e[i].t - m] = MF.e[i^1].f;  
    return 1;
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
      scanf("%lld%lld", &m, &n);
      //init
      ll totEdge = 0, sink = m + n + 1, src = 0;
      //totEdge:總邊數     newP:新結點編號      sink:原圖匯點 

      for (int i = 1; i <= m; ++i)
        scanf("%lld", &row[i]);
      for (int i = 1; i <= n; ++i)
        scanf("%lld", &column[i]);
      for (int i = 1; i <= m; ++i)
        for (int j = 1; j <= n; ++j)
          _up[i][j] = INF, _low[i][j] = 0;
      for (int i = 1; i <= m; ++i) {
        up[++totEdge] = row[i];
        low[totEdge] = row[i];
        u[totEdge] = 0;
        v[totEdge] = i;
      }
      for (int i = 1; i <= n; ++i) {
        up[++totEdge] = column[i];
        low[totEdge] = column[i];
        u[totEdge] = m+i;
        v[totEdge] = sink;
      }
      ll constraint, r, q, V, flag = 1;
      char sign[3];
      scanf("%lld", &constraint);
      for (int i = 1; i <= constraint; ++i) {
        scanf("%lld%lld%s%lld", &r, &q, sign, &V);
        if (sign[0] == '>') {
          V++;
          if (!r && q) {
            for (int j = 1; j <= m; j++) {
              if (V > _up[j][q]) flag = 0;
              _low[j][q] = V;
            }
          }
          else if (r && !q) {
            for (int j = 1; j <= n; ++j) {
              if (V > _up[r][j]) flag = 0;
              _low[r][j] = V;
            }
          }
          else if (!r && !q) {
            for (int j = 1; j <= m; ++j)
              for (int k = 1; k <= n; ++k) {
                if (V > _up[j][k]) flag = 0;
                _low[j][k] = V;
              }
          }
          else {
            if (V > _up[r][q]) flag = 0;
            _low[r][q] = V;
          }
        }
        else if (sign[0] == '=') {
          if (!r && q)
            for (int j = 1; j <= m; j++) {
              if (V < _low[j][q] || V > _up[j][q]) flag = 0;
              _low[j][q] = _up[j][q] =V;
            }
          else if (r && !q)
            for (int j = 1; j <= n; ++j) {
              if (V < _low[r][j] || V > _up[r][j]) flag = 0;
              _low[r][j] = _up[j][q] = V;
            }
          else if (!r && !q)  
            for (int j = 1; j <= m; ++j)
              for (int k = 1; k <= n; ++k) {
                if (V < _low[j][k] || V > _up[j][k]) flag = 0;
                _low[j][k] = _up[j][k] = V;
              }
          else {
            if (V < _low[r][q] || V > _up[r][q]) flag = 0;
            _low[r][q] = _up[r][q] = V;
          }
        }
        else if (sign[0] == '<') {
          V--;
          if (!r && q)
            for (int j = 1; j <= m; j++) {
              if (V < _low[j][q]) flag = 0;
              _up[j][q] = V;
            }
          else if (r && !q)
            for (int j = 1; j <= n; ++j) {
              if (V < _low[r][j]) flag = 0;
              _up[r][j] = V;
            }
          else if (!r && !q)  
            for (int j = 1; j <= n; ++j)
              for (int k = 1; k <= m; ++k) {
                if (V < _low[j][k]) flag = 0;
                _up[j][k] = V;
              }
          else {
            if (V < _low[r][q]) flag = 0;
            _up[r][q] = V;
          }
        }
      }
      for (int i = 1; i <= m; ++i)
        for (int j = 1; j <= n; ++j) {
          up[++totEdge] = _up[i][j];
          low[totEdge] = _low[i][j];
          u[totEdge] = i;
          v[totEdge] = m + j;
        }
      if (flag && lowbound_flow(src, sink, totEdge))         
        for (int i = 1; i <= m; ++i) {
          for (int j = 1; j <= n; ++j)
            printf("%lld ", ans[i][j] + _low[i][j]);
          printf("\n");
        }
      else {
        printf("IMPOSSIBLE\n");
      }
      printf("\n");  
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章