POJ 2396 有上下界有源汇可行流

Budget

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 6282 Accepted: 2387 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 b 代表b列,a b 代表在(a,b)位置的元素

解法:

  • 添加源点s汇点t,由s 向列建边(上下界为该列的和),由行向t建边(上下界为行的和),列向行建边(上下界为指定点的上下界)
  • 添加超级源汇,s向t建一条容量为无穷的边,基本就是模板了

这题题目看错了,导致搞了一天,建图比较坑,特别麻烦

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

typedef pair<int , int> PII ;
const int INF = 0x3f3f3f3f ;
const int N = 1e4 + 11 ;
const int M = 2e6 + 11 ;

struct Edge {
    int next , to ;
    int cap , flow ;
    Edge(){}
    Edge(int a ,int b ,int c) {
        next = a , to = b , cap = flow = c ;
    }
};

int head[N] ; Edge err[M] ; int nedge ;
int dis[N] , gap[N] ; int gnode ;
int super_source , super_sink ,  source , sink , start , dest ;
int n , m ;
vector<PII> stk ;
int need ;

void add_edge(int a ,int b , int c) {
    if(head[a] == -1) ++gnode ;
    if(head[b] == -1) ++gnode ;
    err[++nedge] = Edge(head[a] , b , c) ; head[a] = nedge ;
    err[++nedge] = Edge(head[b] , a , 0) ; head[b] = nedge ;
}

int tot[N] ;
bool init() {
    memset(head , -1 , sizeof(head)) ; nedge = -1 ; gnode = 0 ;
    memset(tot , 0 , sizeof(tot)) ;
    int arr[211][25][2] ;
    int down_up[211][25][2] ;
    int row[211][2] ; int col[25][2] ;
    int cnt = 0 ;
    for(int i = 1 ; i <= n ; ++i)
        for(int j = 1 ; j <= m ; ++j) {
            arr[i][j][0] = ++cnt ;
            arr[i][j][1] = ++cnt ;
            down_up[i][j][0] = 0 ,  down_up[i][j][1] = INF ;
        }
    down_up[0][0][0] = 0 ; down_up[0][0][1] = INF ;
    for(int i = 1 ; i <= n ; ++i) down_up[i][0][0] = 0 , down_up[i][0][1] = INF ;
    for(int j = 1 ; j <= m ; ++j) down_up[0][j][0] = 0 , down_up[0][j][1] = INF ;
    int all = 0 , all2 = 0 ;
    for(int i = 1 ; i <= n ; ++i) row[i][0] = ++cnt , scanf("%d\n" , &row[i][1]) , all += row[i][1] ;
    for(int i = 1 ; i <= m ; ++i) col[i][0] = ++cnt , scanf("%d\n" , &col[i][1]) , all2 += col[i][1] ;
    int st0 = ++cnt , st1 = ++cnt , ed0 = ++cnt ;
    //source = st0 , sink = ed0 ;
    int oper_t ; scanf("%d" ,&oper_t) ;
    bool mark = false ;
    if(all != all2) mark = true ;
    int a , b , c , d ; char op ;
    while(oper_t--) {
        scanf("%d %d %c %d" , &a ,&b ,&op ,&d) ;
        if(mark) continue ;
        if(op == '=') {
            if(down_up[a][b][0] <= d && d <= down_up[a][b][1]) down_up[a][b][0] = down_up[a][b][1] = d ;
            else mark = true ;
            if(d < down_up[0][0][0] || d > down_up[0][0][1] || d < down_up[0][b][0] || d > down_up[0][b][1] || d < down_up[a][0][0] || d > down_up[a][0][1]) mark = true ;
        }
        if(op == '>') {
            ++d ;
            if(d > down_up[a][b][0]) down_up[a][b][0] = d ;
            if(d > down_up[a][b][1]) mark = true ;
            if(d > down_up[0][0][1] || d > down_up[0][b][1] || d > down_up[a][0][1]) mark = true ;
        }
        if(op == '<') {
            --d ;
            if(d < down_up[a][b][1]) down_up[a][b][1] = d ;
            if(d < down_up[a][b][0]) mark = true ;
            if(d < down_up[0][0][0] || d < down_up[0][b][0] || d < down_up[a][0][0]) mark = true ;
        }
    }
    if(mark) return false ;
    for(int i = 1 ; i <= n ; ++i) {
        for(int j = 1 ; j <= m ; ++j) {
            down_up[i][j][0] = max(down_up[i][j][0] , max(down_up[i][0][0] , max(down_up[0][j][0] , down_up[0][0][0]))) ;
            down_up[i][j][1] = min(down_up[i][j][1] , min(down_up[i][0][1] , min(down_up[0][j][1] , down_up[0][0][1]))) ;
        }
    }
    stk.clear() ;
    super_source = 0 , super_sink = ++cnt ;
    add_edge(st0 , st1 , all) ;
    for(int i = 1 ; i <= n ; ++i) {
        tot[st1] -= row[i][1] ;
        tot[row[i][0]] += row[i][1] ;
        for(int j = 1 ; j <= m ; ++j) {
            add_edge(row[i][0] , arr[i][j][0] , INF) ;
            add_edge(arr[i][j][1] , col[j][0] , INF) ;
            stk.push_back(make_pair(nedge+1 , down_up[i][j][0])) ;
            add_edge(arr[i][j][0] , arr[i][j][1] , down_up[i][j][1]-down_up[i][j][0]) ;
            tot[arr[i][j][0]] -= down_up[i][j][0] ;
            tot[arr[i][j][1]] += down_up[i][j][0] ;
        }
    }
    for(int j = 1 ; j <= m ; ++j) {
        tot[col[j][0]] -= col[j][1] ;
        tot[ed0] += col[j][1] ;
    }
    need = 0 ;
    for(int i = 1 ; i < cnt ; ++i) {
        if(tot[i] > 0) {
            add_edge(super_source , i , tot[i]) ;
            need += tot[i] ;
        }
        if(tot[i] < 0) add_edge(i , super_sink , -tot[i]) ;
    }
    add_edge(ed0 , st0 , INF) ;
    return true ;
}

int dfs(int u , int limit) {
    if(u == dest) return limit ;
    int f1 = 0 , f ;
    int minh = gnode-1 ;
    for(int i = head[u] ; i != -1 ; i = err[i].next) {
        int v = err[i].to ;
        if(err[i].flow > 0) {
            if(dis[v]+1 == dis[u]) {
                f = dfs(v , min(limit , err[i].flow)) ;
                err[i].flow -= f ;
                err[i^1].flow += f ;
                limit -= f ;
                f1 += f ;
                if(dis[start] >= gnode) return f1 ;
                if(limit == 0) break ;
            }
            minh = min(minh , dis[v]) ;
        }
    }
    if(f1 == 0) {
        --gap[dis[u]] ;
        if(gap[dis[u]] == 0) dis[start] = gnode ;
        dis[u] = minh + 1 ;
        ++gap[dis[u]] ;
    }
    return f1 ;
}

int sap(int a ,int b) {
    start = a , dest = b ;
    memset(dis , 0 , sizeof(dis)) ;
    memset(gap , 0 , sizeof(gap)) ;
    gap[start] = gnode ;
    int all = 0 ;
    while(dis[start] < gnode) all += dfs(start , INF) ;
    return all ;
}

int main() {
    int t ;
    scanf("%d",&t) ;
    while(t--) {
        scanf("%d%d" ,&n ,&m) ;
        if(init() == false) {
            printf("IMPOSSIBLE\n\n") ;
            continue ;
        }
        if(sap(super_source , super_sink) == need) {
            int idx = 0 ;
            for(int i = 1 ; i <= n ; ++i) {
                for(int j =  1 ; j <= m ; ++j) {
                    printf("%d" , err[stk[idx].first].cap - err[stk[idx].first].flow + stk[idx].second);
                    ++idx ;
                    if(j == m) printf("\n") ;
                    else printf(" ") ;
                }
            }
        }else {
            printf("IMPOSSIBLE\n") ;
        }
        printf("\n") ;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章