POJ2396 Budget
Time Limit: 3000MS | Memory Limit: 65536K | |||
Total Submissions: 7401 | Accepted: 2764 | 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.
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.
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
有源汇有上下界的最大流。
在无源汇有上下界网络流问题中,可以用虚拟源汇,转移最小下界的方式改造图。此处同样可以。
如果从S到T连一条容量为INF的边,那么原容量图也可以看做是无源无汇的图。再虚拟一对源汇ST、ED,根据每个点的度连边,跑Dinic即可。
______
建立n个点代表行,m个点代表列,从某行到某列的连边代表行列交点格子(好像叫矩阵行列式)。按照题目要求限制出每个格子的最大最小值,即是这条边流量的上下界。
跑Dinic之后,统计答案即可。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 #include<queue> 9 using namespace std; 10 const int INF=1e9; 11 const int mxn=100010; 12 int read(){ 13 int x=0,f=1;char ch=getchar(); 14 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 15 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 16 return x*f; 17 } 18 struct edge{ 19 int v,nxt,f; 20 }e[mxn]; 21 int hd[800],mct=1; 22 inline void add_edge(int u,int v,int c){ 23 e[++mct].v=v;e[mct].f=c;e[mct].nxt=hd[u];hd[u]=mct;return; 24 } 25 inline void insert(int u,int v,int c){ 26 // printf("added:%d to %d :%d\n",u,v,c); 27 add_edge(u,v,c);add_edge(v,u,0);return; 28 29 } 30 int n,m,S,T,ST,ED; 31 int in[800]; 32 int d[800]; 33 bool BFS(){ 34 memset(d,0,sizeof d); 35 d[ST]=1; 36 queue<int>q; 37 q.push(ST); 38 while(!q.empty()){ 39 int u=q.front();q.pop(); 40 for(int i=hd[u];i;i=e[i].nxt){ 41 int v=e[i].v; 42 if(!d[v] && e[i].f){ 43 d[v]=d[u]+1;q.push(v); 44 } 45 } 46 } 47 return d[ED]; 48 } 49 int DFS(int u,int lim){ 50 if(u==ED)return lim; 51 int tmp,f=0; 52 for(int i=hd[u];i;i=e[i].nxt){ 53 int v=e[i].v; 54 if(d[v]==d[u]+1 && e[i].f){ 55 tmp=DFS(v,min(lim,e[i].f)); 56 e[i].f-=tmp; 57 e[i^1].f+=tmp; 58 lim-=tmp; 59 f+=tmp; 60 if(!lim)return f; 61 } 62 } 63 d[u]=0; 64 return f; 65 } 66 int Dinic(){ 67 int res=0; 68 while(BFS())res+=DFS(ST,INF); 69 return res; 70 } 71 bool pd(){ 72 for(int i=hd[ST];i;i=e[i].nxt){ 73 if(e[i].f)return 0;//附加边未跑满流,不可行 74 } 75 return 1; 76 } 77 int rsum,csum; 78 int low[210][50],up[210][50]; 79 void init(){ 80 memset(hd,0,sizeof hd); 81 memset(in,0,sizeof in); 82 memset(low,0,sizeof low); 83 memset(up,0x3f,sizeof up); 84 rsum=csum=1; 85 mct=1; 86 return; 87 } 88 bool Build_edge(){ 89 for(int i=1;i<=n;i++) 90 for(int j=1;j<=m;j++){ 91 if(low[i][j]>up[i][j])return 0; 92 in[i]-=low[i][j]; 93 in[j+n]+=low[i][j]; 94 insert(i,j+n,up[i][j]-low[i][j]); 95 } 96 return 1; 97 } 98 int ans[210][50]; 99 bool solve(){ 100 for(int i=0;i<=T;i++){ 101 if(in[i]>0) insert(ST,i,in[i]); 102 else if(in[i]<0)insert(i,ED,-in[i]); 103 } 104 insert(T,S,INF); 105 // printf("%d\n",Dinic()); 106 Dinic(); 107 if(!pd())return 0; 108 for(int i=1;i<=n;i++){ 109 for(int j=hd[i];j;j=e[j].nxt){ 110 int v=e[j].v; 111 ans[i][v-n]=e[j^1].f+low[i][v-n]; 112 } 113 } 114 return 1; 115 } 116 117 int main(){ 118 int i,j; 119 int Cas=read(); 120 while(Cas--){ 121 n=read();m=read(); 122 int x,y,w; 123 S=0;T=n+m+1;//源汇 124 ST=T+1;ED=ST+1;//超级源汇 125 init(); 126 for(i=1;i<=n;i++){ 127 x=read(); 128 in[S]-=x; in[i]+=x; 129 rsum+=x; 130 } 131 for(i=1;i<=m;i++){ 132 x=read(); 133 in[i+n]-=x; in[T]+=x; 134 csum+=x; 135 } 136 int c=read();char op[3]; 137 while(c--){ 138 scanf("%d%d%s%d",&x,&y,op,&w); 139 int s1=x,t1=x; 140 int s2=y,t2=y; 141 if(x==0){s1=1;t1=n;} 142 if(y==0){s2=1;t2=m;} 143 for(i=s1;i<=t1;i++) 144 for(j=s2;j<=t2;j++){ 145 switch(op[0]){ 146 case '=':{low[i][j]=max(w,low[i][j]);up[i][j]=min(w,up[i][j]);break;} 147 case '>':{low[i][j]=max(w+1,low[i][j]);break;} 148 case '<':{up[i][j]=min(w-1,up[i][j]);break;} 149 } 150 } 151 } 152 if(rsum!=csum || !Build_edge()){printf("IMPOSSIBLE\n\n");continue;} 153 if(!solve()){printf("IMPOSSIBLE\n\n");continue;} 154 for(i=1;i<=n;i++){ 155 for(j=1;j<=m;j++) 156 printf("%d ",ans[i][j]); 157 printf("\n"); 158 } 159 printf("\n"); 160 } 161 return 0; 162 }
本文为博主原创文章,转载请注明出处。