• POJ 2396 Budget


    Budget

    Time Limit: 3000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 2396
    64-bit integer IO format: %lld      Java class name: Main
     
    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

     
    解题:贫道正在赶。。。这题建图正尼玛。。。。
     
    绝世好。。。啊。。
     
    有源汇的最大流,把原来的汇点到源点连一条无穷的边,然后根据无源汇求建图的方法,建立超级源汇点,满流,则最大流就是此有源汇的上下界最大流。
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <climits>
      7 #include <vector>
      8 #include <queue>
      9 #include <cstdlib>
     10 #include <string>
     11 #include <set>
     12 #include <stack>
     13 #define LL long long
     14 #define pii pair<int,int>
     15 #define INF 0x3f3f3f3f
     16 using namespace std;
     17 const int maxn = 500;
     18 int d[maxn],low[maxn][maxn],high[maxn][maxn];
     19 int tot,row,col,colsum[maxn],rowsum[maxn],S,T;
     20 int ans[maxn][maxn],flow[maxn][maxn],de[maxn];
     21 bool bfs() {
     22     memset(d,-1,sizeof(d));
     23     queue<int>q;
     24     d[S] = 1;
     25     q.push(S);
     26     while(!q.empty()) {
     27         int u = q.front();
     28         q.pop();
     29         for(int i = 0; i <= T; ++i)
     30             if(flow[u][i] && d[i] == -1) {
     31                 d[i] = d[u] + 1;
     32                 q.push(i);
     33             }
     34     }
     35     return d[T] > -1;
     36 }
     37 int dfs(int u,int low) {
     38     if(u == T) return low;
     39     int tmp = 0,a;
     40     for(int i = 0; i <= T; ++i) {
     41         if(flow[u][i] && d[i] == d[u] + 1&&(a=dfs(i,min(low,flow[u][i])))) {
     42             flow[u][i] -= a;
     43             flow[i][u] += a;
     44             low -= a;
     45             tmp += a;
     46             if(!low) break;
     47         }
     48     }
     49     if(!tmp) d[u] = -1;
     50     return tmp;
     51 }
     52 int dinic(){
     53     int ans = 0;
     54     while(bfs()) ans += dfs(S,INF);
     55     return ans;
     56 }
     57 int main() {
     58     int cs,con;
     59     bool cao = false;
     60     scanf("%d",&cs);
     61     while(cs--) {
     62         if(cao) puts("");
     63         cao = true;
     64         scanf("%d %d",&row,&col);
     65         for(int i = 1; i <= row; ++i) scanf("%d",rowsum+i);
     66         for(int i = 1; i <= col; ++i) scanf("%d",colsum+i);
     67         memset(low,0,sizeof(low));
     68         memset(flow,0,sizeof(flow));
     69         memset(de,0,sizeof(de));
     70         for(int i = 1; i <= row; ++i)
     71             for(int j = 1; j <= col; ++j)
     72                 high[i][row + j] = INF;
     73         scanf("%d",&con);
     74         while(con--) {
     75             int u,v,w;
     76             char op[5];
     77             scanf("%d %d %s %d",&u,&v,op,&w);
     78             if(!u && v) {
     79                 if(op[0] == '=') {
     80                     for(int i = 1; i <= row; ++i)
     81                         high[i][v+row] = low[i][v+row] = w;
     82                 } else if(op[0] == '>') {
     83                     for(int i = 1; i <= row; ++i)
     84                         low[i][v+row] = max(low[i][v+row],w+1);
     85                 } else if(op[0] == '<') {
     86                     for(int i = 1; i <= row; ++i)
     87                         high[i][v+row] = min(high[i][v+row],w-1);
     88                 }
     89             } else if(u && !v) {
     90                 if(op[0] == '=') {
     91                     for(int i = 1; i <= col; ++i)
     92                         low[u][i+row] = high[u][i+row] = w;
     93                 } else if(op[0] == '>') {
     94                     for(int i = 1; i <= col; ++i)
     95                         low[u][i+row] = max(low[u][i+row],w+1);
     96                 } else if(op[0] == '<') {
     97                     for(int i = 1; i <= col; ++i)
     98                         high[u][i+row] = min(high[u][i+row],w-1);
     99                 }
    100             } else if(!u && !v) {
    101                 if(op[0] == '=') {
    102                     for(int i = 1; i <= row; ++i)
    103                         for(int j = 1; j <= col; ++j)
    104                             low[i][j+row] = high[i][j+row] = w;
    105                 } else if(op[0] == '>') {
    106                     for(int i = 1; i <= row; ++i)
    107                         for(int j = 1; j <= col; ++j)
    108                             low[i][j+row] = max(low[i][j+row],w+1);
    109                 } else if(op[0] == '<') {
    110                     for(int i = 1; i <= row; ++i)
    111                         for(int j = 1; j <= col; ++j)
    112                             high[i][j+row] = min(high[i][j+row],w-1);
    113                 }
    114             } else if(u && v) {
    115                 if(op[0] == '=') low[u][v+row] = high[u][v+row] = w;
    116                 else if(op[0] == '>') low[u][v+row] = max(low[u][v+row],w+1);
    117                 else if(op[0] == '<') high[u][v+row] = min(high[u][v+row],w-1);
    118             }
    119         }
    120         S = 0;
    121         T = row + col + 1;
    122         for(int i = 1; i <= row; ++i)
    123             low[S][i] = high[S][i] = rowsum[i];
    124         for(int i = 1; i <= col; ++i)
    125             low[i+row][T] = high[i+row][T] = colsum[i];
    126         int SS = T+1,TT = T + 2,sum = 0;
    127         for(int i = 1; i <= row; ++i)
    128             for(int j = 1; j <= col; ++j){
    129                 flow[i][j+row] = high[i][j+row] - low[i][j+row];
    130                 de[i] -= low[i][j+row];
    131                 de[j+row] += low[i][j+row];
    132             }
    133         for(int i = 1; i <= row; ++i){
    134             flow[S][i] = high[S][i] - low[S][i];
    135             de[S] -= low[S][i];
    136             de[i] += low[S][i];
    137         }
    138         for(int j = 1; j <= col; ++j){
    139             flow[j+row][T] = high[j+row][T] - low[j+row][T];
    140             de[j+row] -= low[j+row][T];
    141             de[T] += low[j+row][T];
    142         }
    143         for(int i = 0; i <= T; ++i){
    144             if(de[i] > 0) {sum += de[i];flow[SS][i] = de[i];}
    145             if(de[i] < 0) flow[i][TT] = -de[i];
    146         }
    147         flow[T][S] = INF;
    148         S = SS;
    149         T = TT;
    150         if(dinic() == sum){
    151             S = 0;
    152             T = row + col + 1;
    153             dinic();
    154             for(int i = 1; i <= row; ++i)
    155                 for(int j = 1; j <= col; ++j)
    156                     printf("%d%c",flow[j+row][i]+low[i][row+j],j == col?'
    ':' ');
    157         }else puts("IMPOSSIBLE");
    158     }
    159     return 0;
    160 }
    View Code
     
  • 相关阅读:
    【HDU6609】Find the answer【线段树】
    【HDU6602】Longest Subarray【线段树+分治】
    PCIe
    NVMe Windows 支持情况
    PCIe/NVMe Soft Protocol Analyzer
    I am coming..
    hibernate自动建表技术_采用数据库反向生成技术
    struts2的执行流程
    oracle中scott用户下四个基本表SQL语句练习
    SQL语句中的having和where的区别
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4080095.html
Copyright © 2020-2023  润新知