【POJ2396】Budget(上下界网络流)
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 > 10Sample Output
2 3 3 3 3 4 IMPOSSIBLE
【题意】
有一个n*m的方阵,里面的数字未知,但是我们知道如下约束条件:
每一行的数字的和
每一列的数字的和
某些格子有特殊的大小约束,用大于号,小于号和等于号表示
问:是否存在用正数填充这个方阵的方案,满足所有的约束,若有,输出之,否则输出IMPOSSIBLE。
【分析】
这题是有有源汇的上下界可行流,具体可看:http://blog.csdn.net/water_glass/article/details/6823741
求解一个有上下界的网络流的步骤:
1.首先进行构图,对于那么对流量没有限制的边,我们直接将容量赋值为原始的容量,而对于有流量要求的边,我们将容量减去下界并将其等价与无下界的边。最后就是添加一个附
加汇点和一个附加源点,从附加源点连向每个顶点的容量为以该点所有流入的下界流量总和,每个顶点流向附加汇点是该点流出的下界流量总和。
2.我们要添加一条从汇点到源点流量为INF的边,这条边的意义在于,能够使得源点会汇点满足成为流量平衡条件的普通节点。(以下为有上下界的最小流求解步骤)
3.我们在以附加源点和附加汇点求一次最大流,如果所有的到附加汇点的边都满载,那么说明这个网络是存在满足所有下界的可行流的。因为去除了下界容量的图具备这个能力。但
是此时的可行流(从汇点流向源点的流量)并不一定是最小流,因为满足情况的可行流是不唯一的。
4.紧接着,我们在原图上从汇点向源点求一次最大流(此时要删除掉那条从汇点到源点的INF的边),此时便是一个缩流的过程,旨在试探图中是否还存在流量去替代汇点到源点的流量。这里计算出来的结果可能比我们已得到的可行流还要大,意思是说从汇点到源点有的是空间,因此也就不必连接那条INF的边了,整个网络的流量可以为0,网络中存在环流。由于这里免不了会进行删边的操作,因此我们直接找到那条边,把流量赋值为0就可以了。
上述来自:http://www.cnblogs.com/proverbs/archive/2013/01/05/2846910.html
对于循环流上的某一条边x->y,下界为k1,上界为k2,我们这样变形,变成一个只有上界的网络流(下界默认为0),即普通的网络流,满流时有解。
对于原图源汇的,我们在原图汇点连向原图源点一条流量为INF的边,形成循环流即可。
关于此题建模:类似二分图,每行建一个点,每列建一个点,源点连行,流量为这行的和,列连汇点,流量为这列的和。
行连向列的流量表示这行这列这个点的值。
根据题目中的约束求出边的上下界,跑一遍即可。
代码如下:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 using namespace std; 8 #define INF 0xfffffff 9 #define Maxn 210 10 #define Maxm 30 11 12 int lim[2][Maxn][Maxm]; 13 14 struct node 15 { 16 int x,y,next,f,o; 17 }t[Maxn*Maxm*10];int len; 18 19 int first[Maxn*Maxn+110],dis[Maxn*Maxm+110]; 20 int st,ed,fst,fed,n,m,sum; 21 bool ok; 22 23 int mymax(int x,int y) {return x>y?x:y;} 24 int mymin(int x,int y) {return x<y?x:y;} 25 26 void ins(int x,int y,int f) 27 { 28 if(f==0) return; 29 if(x==fst) sum+=f; 30 t[++len].x=x;t[len].y=y;t[len].f=f; 31 t[len].next=first[x];first[x]=len;t[len].o=len+1; 32 t[++len].x=y;t[len].y=x;t[len].f=0; 33 t[len].next=first[y];first[y]=len;t[len].o=len-1; 34 } 35 36 void get_e(int x,int y,int l1,int l2) 37 { 38 if(l2<l1) {ok=0;return;} 39 ins(fst,y,l1);ins(x,fed,l1);ins(x,y,l2-l1); 40 } 41 42 void init() 43 { 44 scanf("%d%d",&n,&m); 45 memset(first,0,sizeof(first)); 46 len=0; 47 st=n+m+1;ed=st+1;fst=ed+1,fed=fst+1; 48 ok=1;sum=0; 49 for(int i=1;i<=n;i++) 50 { 51 int x; 52 scanf("%d",&x); 53 get_e(st,i,x,x); 54 } 55 for(int i=1;i<=m;i++) 56 { 57 int x; 58 scanf("%d",&x); 59 get_e(i+n,ed,x,x); 60 } 61 int q; 62 scanf("%d",&q); 63 char s[10]; 64 for(int i=1;i<=n;i++) 65 for(int j=1;j<=m;j++) 66 { 67 lim[0][i][j]=0;lim[1][i][j]=INF; 68 } 69 while(q--) 70 { 71 int x,y,z; 72 scanf("%d%d%s%d",&x,&y,s,&z); 73 int k1,k2,k3,k4; 74 if(x==0) k1=1,k2=n; 75 else k1=k2=x; 76 if(y==0) k3=1,k4=m; 77 else k3=k4=y; 78 for(int i=k1;i<=k2;i++) 79 for(int j=k3;j<=k4;j++) 80 { 81 if(s[0]=='>') lim[0][i][j]=mymax(lim[0][i][j],z+1); 82 if(s[0]=='=') lim[0][i][j]=mymax(lim[0][i][j],z); 83 if(s[0]=='<') lim[1][i][j]=mymin(lim[1][i][j],z-1); 84 if(s[0]=='=') lim[1][i][j]=mymin(lim[1][i][j],z); 85 } 86 } 87 for(int i=1;i<=n;i++) 88 for(int j=1;j<=m;j++) 89 { 90 get_e(i,j+n,lim[0][i][j],lim[1][i][j]); 91 if(ok==0) break; 92 } 93 get_e(ed,st,0,INF); 94 } 95 96 queue<int > q; 97 bool bfs() 98 { 99 while(!q.empty()) q.pop(); 100 memset(dis,-1,sizeof(dis)); 101 q.push(fst);dis[fst]=0; 102 while(!q.empty()) 103 { 104 int x=q.front();q.pop(); 105 for(int i=first[x];i;i=t[i].next) if(t[i].f>0) 106 { 107 if(dis[t[i].y]==-1) 108 { 109 dis[t[i].y]=dis[x]+1; 110 q.push(t[i].y); 111 } 112 } 113 } 114 if(dis[fed]==-1) return 0; 115 return 1; 116 } 117 118 int ffind(int x,int low) 119 { 120 if(x==fed) return low; 121 int now=0; 122 for(int i=first[x];i;i=t[i].next) if(t[i].f>0) 123 { 124 int y=t[i].y; 125 if(now==low) break; 126 if(dis[y]==dis[x]+1) 127 { 128 int a=mymin(t[i].f,low-now); 129 a=ffind(t[i].y,a); 130 t[i].f-=a; 131 t[t[i].o].f+=a; 132 now+=a; 133 } 134 } 135 if(now==0) dis[x]=-1; 136 return now; 137 } 138 139 int pri[Maxn][Maxm]; 140 void dinic() 141 { 142 int ans=0; 143 while(bfs()) 144 { 145 ans+=ffind(fst,INF); 146 } 147 memset(pri,0,sizeof(pri)); 148 if(ans==sum) 149 { 150 for(int i=2;i<=len;i+=2) 151 { 152 if(t[i].x>n&&t[i].x<=n+m&&t[i].y<=n+m) 153 { 154 pri[t[i].y][t[i].x-n]=t[i].f; 155 } 156 } 157 for(int i=1;i<=n;i++) 158 { 159 for(int j=1;j<=m;j++) 160 printf("%d ",pri[i][j]+lim[0][i][j]); 161 printf("\n"); 162 } 163 } 164 else printf("IMPOSSIBLE\n"); 165 } 166 167 int main() 168 { 169 int T; 170 scanf("%d",&T); 171 while(T--) 172 { 173 init(); 174 if(ok==0) {printf("IMPOSSIBLE\n\n");continue;} 175 dinic();printf("\n"); 176 } 177 return 0; 178 }
2016-05-19 17:01:02