博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ2396】Budget(上下界网络流)
阅读量:4624 次
发布时间:2019-06-09

本文共 5604 字,大约阅读时间需要 18 分钟。

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

22 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
 
【题意】

  有一个n*m的方阵,里面的数字未知,但是我们知道如下约束条件:

  每一行的数字的和

  每一列的数字的和

  某些格子有特殊的大小约束,用大于号,小于号和等于号表示

  问:是否存在用正数填充这个方阵的方案,满足所有的约束,若有,输出之,否则输出IMPOSSIBLE。

 

【分析】

  这题是有有源汇的上下界可行流,具体可看:

  

求解一个有上下界的网络流的步骤:

1.首先进行构图,对于那么对流量没有限制的边,我们直接将容量赋值为原始的容量,而对于有流量要求的边,我们将容量减去下界并将其等价与无下界的边。最后就是添加一个附

加汇点和一个附加源点,从附加源点连向每个顶点的容量为以该点所有流入的下界流量总和,每个顶点流向附加汇点是该点流出的下界流量总和。

2.我们要添加一条从汇点到源点流量为INF的边,这条边的意义在于,能够使得源点会汇点满足成为流量平衡条件的普通节点。

(以下为有上下界的最小流求解步骤)

3.我们在以附加源点和附加汇点求一次最大流,如果所有的到附加汇点的边都满载,那么说明这个网络是存在满足所有下界的可行流的。因为去除了下界容量的图具备这个能力。但
 是此时的可行流(从汇点流向源点的流量)并不一定是最小流,因为满足情况的可行流是不唯一的。

4.紧接着,我们在原图上从汇点向源点求一次最大流(此时要删除掉那条从汇点到源点的INF的边),此时便是一个缩流的过程,旨在试探图中是否还存在流量去替代汇点到源点的流量。这里计算出来的结果可能比我们已得到的可行流还要大,意思是说从汇点到源点有的是空间,因此也就不必连接那条INF的边了,整个网络的流量可以为0,网络中存在环流。

由于这里免不了会进行删边的操作,因此我们直接找到那条边,把流量赋值为0就可以了。

上述来自:

 

  对于循环流上的某一条边x->y,下界为k1,上界为k2,我们这样变形,变成一个只有上界的网络流(下界默认为0),即普通的网络流,满流时有解。

 

  对于原图源汇的,我们在原图汇点连向原图源点一条流量为INF的边,形成循环流即可。

 

  关于此题建模:类似二分图,每行建一个点,每列建一个点,源点连行,流量为这行的和,列连汇点,流量为这列的和。

  行连向列的流量表示这行这列这个点的值。

  根据题目中的约束求出边的上下界,跑一遍即可。

 

代码如下:

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
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
') 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
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 }
[POJ2396]

 

2016-05-19 17:01:02

 

转载于:https://www.cnblogs.com/Konjakmoyu/p/5509300.html

你可能感兴趣的文章
leetcode笔记:Pascal&#39;s Triangle
查看>>
ASP.NET性能优化之构建自定义文件缓存
查看>>
Shell——windows上写完放入linux的时候需要注意的问题
查看>>
65条常用的正则表达式
查看>>
Vscode断点调试PHP
查看>>
做前端要做的6大事
查看>>
LeetCode 813. Largest Sum of Averages
查看>>
vSphere、Hyper-V与XenServer 你选哪个?
查看>>
java.lang.UnsupportedClassVersionError
查看>>
实现接口必须要加注解@Override吗
查看>>
apicloud UISearchBar 使用方法
查看>>
【spring+websocket的使用】
查看>>
mongo二维数组操作
查看>>
localStorage之本地储存
查看>>
Archlinux 交换左Ctrl和Cap键
查看>>
#openstack故障处理汇总
查看>>
搜索旋转排序数组 II
查看>>
20、docker swarm
查看>>
psp工具软件前景与范围文档
查看>>
day06-三元表达式
查看>>