【POJ2396】Budget(上下界网络流)
2016-05-19 16:59
351 查看
Description
Wearesupposedtomakeabudgetproposalforthismulti-sitecompetition.Thebudgetproposalisamatrixwheretherowsrepresentdifferentkindsofexpensesandthecolumnsrepresentdifferentsites.Wehadameetingaboutthis,sometimeagowherewediscussedthesumsoverdifferentkindsofexpensesandsumsoverdifferentsites.Therewasalsosometalkaboutspecialconstraints:someonementionedthatComputerCenterwouldneedatleast2000KRialsforfoodandsomeonefromSharifAuthoritiesarguedtheywouldn'tusemorethan30000KRialsforT-shirts.Anyway,wearesuretherewasmore;wewillgoandtrytofindsomenotesfromthatmeeting.
And,bytheway,noonereallyreadsbudgetproposalsanyway,sowe'lljusthavetomakesurethatitsumsupproperlyandmeetsallconstraints.
Input
ThefirstlineoftheinputcontainsanintegerN,givingthenumberoftestcases.Thenextlineisempty,then,testcasesfollow:Thefirstlineofeachtestcasecontainstwointegers,mandn,givingthenumberofrowsandcolumns(m<=200,n<=20).Thesecondlinecontainsmintegers,givingtherowsumsofthematrix.Thethirdlinecontainsnintegers,givingthecolumnsumsofthematrix.Thefourthlinecontainsanintegerc(c<1000)givingthenumberofconstraints.Thenextclinescontaintheconstraints.Thereisanemptylineaftereachtestcase.
Eachconstraintconsistsoftwointegersrandq,specifyingsomeentry(orentries)inthematrix(theupperleftcorneris11and0isinterpretedas"ALL",i.e.40meansallentriesonthefourthrowand00meanstheentirematrix),oneelementfromtheset{<,=,>}andoneintegerv,withtheobviousinterpretation.Forinstance,theconstraint12>5meansthatthecellinthe1strowand2ndcolumnmusthaveanentrystrictlygreaterthan5,andtheconstraint40=3meansthatallelementsinthefourthrowshouldbeequalto3.
Output
Foreachcaseoutputamatrixofnon-negativeintegersmeetingtheaboveconstraintsorthestring"IMPOSSIBLE"ifnolegalsolutionexists.Putoneemptylinebetweenmatrices.
SampleInput
2 23 810 567 4 02>2 21=3 23>2 23<5 22 45 67 1 11>10
SampleOutput
233 334 IMPOSSIBLE
【题意】
有一个n*m的方阵,里面的数字未知,但是我们知道如下约束条件:
每一行的数字的和
每一列的数字的和
某些格子有特殊的大小约束,用大于号,小于号和等于号表示
问:是否存在用正数填充这个方阵的方案,满足所有的约束,若有,输出之,否则输出IMPOSSIBLE。
【分析】
这题是有有源汇的上下界可行流,具体可看:
求解一个有上下界的网络流的步骤:
1.首先进行构图,对于那么对流量没有限制的边,我们直接将容量赋值为原始的容量,而对于有流量要求的边,我们将容量减去下界并将其等价与无下界的边。最后就是添加一个附
加汇点和一个附加源点,从附加源点连向每个顶点的容量为以该点所有流入的下界流量总和,每个顶点流向附加汇点是该点流出的下界流量总和。
2.我们要添加一条从汇点到源点流量为INF的边,这条边的意义在于,能够使得源点会汇点满足成为流量平衡条件的普通节点。
(以下为有上下界的最小流求解步骤)
3.我们在以附加源点和附加汇点求一次最大流,如果所有的到附加汇点的边都满载,那么说明这个网络是存在满足所有下界的可行流的。因为去除了下界容量的图具备这个能力。但
是此时的可行流(从汇点流向源点的流量)并不一定是最小流,因为满足情况的可行流是不唯一的。
4.紧接着,我们在原图上从汇点向源点求一次最大流(此时要删除掉那条从汇点到源点的INF的边),此时便是一个缩流的过程,旨在试探图中是否还存在流量去替代汇点到源点的流量。这里计算出来的结果可能比我们已得到的可行流还要大,意思是说从汇点到源点有的是空间,因此也就不必连接那条INF的边了,整个网络的流量可以为0,网络中存在环流。
由于这里免不了会进行删边的操作,因此我们直接找到那条边,把流量赋值为0就可以了。
上述来自:
对于循环流上的某一条边x->y,下界为k1,上界为k2,我们这样变形,变成一个只有上界的网络流(下界默认为0),即普通的网络流,满流时有解。
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<queue> usingnamespacestd; #defineINF0xfffffff #defineMaxn210 #defineMaxm30 intlim[2][Maxn][Maxm]; structnode { intx,y,next,f,o; }t[Maxn*Maxm*10];intlen; intfirst[Maxn*Maxn+110],dis[Maxn*Maxm+110]; intst,ed,fst,fed,n,m,sum; boolok; intmymax(intx,inty){returnx>y?x:y;} intmymin(intx,inty){returnx<y?x:y;} voidins(intx,inty,intf) { if(f==0)return; if(x==fst)sum+=f; t[++len].x=x;t[len].y=y;t[len].f=f; t[len].next=first[x];first[x]=len;t[len].o=len+1; t[++len].x=y;t[len].y=x;t[len].f=0; t[len].next=first[y];first[y]=len;t[len].o=len-1; } voidget_e(intx,inty,intl1,intl2) { if(l2<l1){ok=0;return;} ins(fst,y,l1);ins(x,fed,l1);ins(x,y,l2-l1); } voidinit() { scanf("%d%d",&n,&m); memset(first,0,sizeof(first)); len=0; st=n+m+1;ed=st+1;fst=ed+1,fed=fst+1; ok=1;sum=0; for(inti=1;i<=n;i++) { intx; scanf("%d",&x); get_e(st,i,x,x); } for(inti=1;i<=m;i++) { intx; scanf("%d",&x); get_e(i+n,ed,x,x); } intq; scanf("%d",&q); chars[10]; for(inti=1;i<=n;i++) for(intj=1;j<=m;j++) { lim[0][i][j]=0;lim[1][i][j]=INF; } while(q--) { intx,y,z; scanf("%d%d%s%d",&x,&y,s,&z); intk1,k2,k3,k4; if(x==0)k1=1,k2=n; elsek1=k2=x; if(y==0)k3=1,k4=m; elsek3=k4=y; for(inti=k1;i<=k2;i++) for(intj=k3;j<=k4;j++) { if(s[0]=='>')lim[0][i][j]=mymax(lim[0][i][j],z+1); if(s[0]=='=')lim[0][i][j]=mymax(lim[0][i][j],z); if(s[0]=='<')lim[1][i][j]=mymin(lim[1][i][j],z-1); if(s[0]=='=')lim[1][i][j]=mymin(lim[1][i][j],z); } } for(inti=1;i<=n;i++) for(intj=1;j<=m;j++) { get_e(i,j+n,lim[0][i][j],lim[1][i][j]); if(ok==0)break; } get_e(ed,st,0,INF); } queue<int>q; boolbfs() { while(!q.empty())q.pop(); memset(dis,-1,sizeof(dis)); q.push(fst);dis[fst]=0; while(!q.empty()) { intx=q.front();q.pop(); for(inti=first[x];i;i=t[i].next)if(t[i].f>0) { if(dis[t[i].y]==-1) { dis[t[i].y]=dis[x]+1; q.push(t[i].y); } } } if(dis[fed]==-1)return0; return1; } intffind(intx,intlow) { if(x==fed)returnlow; intnow=0; for(inti=first[x];i;i=t[i].next)if(t[i].f>0) { inty=t[i].y; if(now==low)break; if(dis[y]==dis[x]+1) { inta=mymin(t[i].f,low-now); a=ffind(t[i].y,a); t[i].f-=a; t[t[i].o].f+=a; now+=a; } } if(now==0)dis[x]=-1; returnnow; } intpri[Maxn][Maxm]; voiddinic() { intans=0; while(bfs()) { ans+=ffind(fst,INF); } memset(pri,0,sizeof(pri)); if(ans==sum) { for(inti=2;i<=len;i+=2) { if(t[i].x>n&&t[i].x<=n+m&&t[i].y<=n+m) { pri[t[i].y][t[i].x-n]=t[i].f; } } for(inti=1;i<=n;i++) { for(intj=1;j<=m;j++) printf("%d",pri[i][j]+lim[0][i][j]); printf("\n"); } } elseprintf("IMPOSSIBLE\n"); } intmain() { intT; scanf("%d",&T); while(T--) { init(); if(ok==0){printf("IMPOSSIBLE\n\n");continue;} dinic();printf("\n"); } return0; }
[POJ2396]
2016-05-1917:01:02
相关文章推荐
- iOS音频的后台播放总结(后台网络请求歌曲,Remote控制,锁屏封面,各种打断)
- 融云在网络切换的情况,怎么刷新列表
- Android开发之网络通讯详解
- Android学习笔记三十:socket网络通信基础(示例,出错代码)
- Android中使用HTTP服务
- 无处不在的网络互联与绿色数据中心
- TCP洪水攻击(SYN Flood)的诊断和处理
- iOS学习39网络之数据请求
- xcode7.0https安全协议
- AJAX请求在微信环境报错:NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
- PXE+HTTP+TFP+DHCP自动化部署
- web大文件上传控件-监控fd_create流程-Xproer.HttpUploader6
- 网络流二十四题之十六 —— 数字梯形问题(DIGIT)
- Python网络编程10----django分页
- Retrofit文件上传与下载(xjh 这应该是retrofit1的代码 亲测不行)___http://www.ithao123.cn/content-7813575.html
- ExpandableListView使用,点击条目请求网络数据,箭头在右边
- 图解Wireshark协议分析实例
- IIS http 错误 401.3 - unauthorized
- HttpOnly 与cookie安全
- okHttp 使用HTTPS证书