算法复习——有源汇上下界可行流(bzoj2396)
题目:
Description
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
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
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
题解:
算是求有源汇上下界最大流的一道模板题···只不过建图有点麻烦····
先说有源汇上下界最大流最小流的基本求法(引用自农民伯伯-Coding):
有源汇网络的可行流
对于流量有上下界的有源汇网络,原图中存在源点S和汇点T,为了求可行流,先将其转换为无源汇网络。
从T-->S引入一条边,其流量上下界为[0, INF],此时原图变为了无源汇网络,然后按照无源汇网络求解可行流的方法来求解。
有源汇网络的最大流
要求最大流,先求可行流,通过“有源汇网络的可行流”的求解方法来判断有源汇网络存在可行流。
若存在可行流,记从S流出的流量sum1,然后将T-->S的边取消,再次从S到T求解网络的最大流,记从S流出的流量sum2. 那么该有源汇网络的最大流为 sum1 + sum2.
其中,sum1是在网络满足流量下界的条件下,从源点S流出的流量;求出sum1之后,网络中可能还有余量可以继续增广,那么再次求解从S到T的最大流,得到sum2,sum1 + sum2即为最终的最大流。
有源汇网络的最小流
求解有源汇网络最小流分为以下几步:
(1)对SS到TT求一次最大流,即为f1.(在有源汇的情况下,先把整个网络趋向必须边尽量满足的情况);
(2)添加一条边T-->S,流量上限为INF,这条边即为P.(构造无源网络)
(3)对SS到TT再次求最大流,即为f2。(判断此时的网络中存在可行流,同时求出SS-->TT最大流)
如果所有必须边都满流,证明存在可行解,原图的最小流为“流经边P的流量”(原图已经构造成无源汇网络,对于S同样满足 入流和==出流和,只有新添加的边流向S,而S的出流就是原图的最小流)。
注: 最小流求法的正确性不知如何证明,待继续学习。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=;
const int M=;
const int inf=0x3f3f3f3f;
int tot=,first[N],next[M],go[M],rest[M],lev[N],cur[N];
int tr[N],tc[N],n,m,low[N][N],up[N][N],T,src,des,ss,tt,sum1,sum2,k,ans,id[N][N],p;
bool jud[N][N];
inline void comb(int a,int b,int c)
{
next[++tot]=first[a],first[a]=tot,go[tot]=b,rest[tot]=c;
next[++tot]=first[b],first[b]=tot,go[tot]=a,rest[tot]=;
}
inline void comb1(int a,int b,int c)
{
next[tot]=first[a],first[a]=tot,go[tot]=b,rest[tot]=c;
next[++tot]=first[b],first[b]=tot,go[tot]=a,rest[tot]=;
}
inline int R()
{
char c;
int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar())
f=(f<<)+(f<<)+c-'';
return f;
}
inline void pre()
{
memset(first,,sizeof(first));
memset(low,,sizeof(low));
memset(up,inf,sizeof(up));
memset(tr,,sizeof(tr));
memset(tc,,sizeof(tc));
tot=,src=,des=n+m+,ss=n+m+,tt=n+m+,sum1=,sum2=,ans=;
}
inline bool bfs()
{
for(int i=src;i<=tt;i++) cur[i]=first[i],lev[i]=-;
static int que[N],tail,u,v;
que[tail=]=ss;
lev[ss]=;
for(int head=;head<=tail;head++)
{
u=que[head];
for(int e=first[u];e;e=next[e])
{
if(lev[v=go[e]]==-&&rest[e])
{
lev[v]=lev[u]+;
que[++tail]=v;
if(v==tt) return true;
}
}
}
return false;
}
inline int dinic(int u,int flow)
{
if(u==tt)
return flow;
int res=,delta,v;
for(int &e=cur[u];e;e=next[e])
{
if(lev[v=go[e]]>lev[u]&&rest[e])
{
delta=dinic(v,min(flow-res,rest[e]));
if(delta)
{
rest[e]-=delta;
rest[e^]+=delta;
res+=delta;
if(res==flow) break;
}
}
}
if(flow!=res) lev[u]=-;
return res;
}
inline void maxflow()
{
while(bfs())
ans+=dinic(ss,inf);
}
int main()
{
//freopen("a.in","r",stdin);
T=R();
int a,b,c;
char s[];
while(T--)
{
scanf("\n");
n=R(),m=R();
pre();
for(int i=;i<=n;i++)
{
a=R();
tc[src]+=a,tr[i]+=a;
sum1+=a;
}
for(int i=;i<=m;i++)
{
a=R();
tr[des]+=a,tc[n+i]+=a;
sum2+=a;
}
if(sum1!=sum2)
{
cout<<"IMPOSSIBLE"<<endl;
cout<<endl;
continue;
}
k=R();
while(k--)
{
scanf("%d%d%s%d",&a,&b,s,&c);
if(a==&&b==)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(s[]=='=')
{
low[i][j]=max(low[i][j],c);
up[i][j]=min(up[i][j],c);
}
if(s[]=='<')
up[i][j]=min(up[i][j],c-);
if(s[]=='>')
low[i][j]=max(low[i][j],c+);
}
}
else if(a==)
for(int i=;i<=n;i++)
{
if(s[]=='=')
{
low[i][b]=max(low[i][b],c);
up[i][b]=min(up[i][b],c);
}
if(s[]=='<')
up[i][b]=min(up[i][b],c-);
if(s[]=='>')
low[i][b]=max(low[i][b],c+);
}
else if(b==)
for(int i=;i<=m;i++)
{
if(s[]=='=')
{
low[a][i]=max(low[a][i],c);
up[a][i]=min(up[a][i],c);
}
if(s[]=='<')
up[a][i]=min(up[a][i],c-);
if(s[]=='>')
low[a][i]=max(low[a][i],c+);
}
else
{
if(s[]=='=')
{
low[a][b]=max(low[a][b],c);
up[a][b]=min(up[a][b],c);
}
if(s[]=='<')
up[a][b]=min(up[a][b],c-);
if(s[]=='>')
low[a][b]=max(low[a][b],c+);
}
}
bool flag=false;
for(int i=;i<=n;i++)
{
if(flag==true) break;
for(int j=;j<=m;j++)
{
if(low[i][j]>up[i][j])
{
flag=true;
break;
}
else
{
tr[n+j]+=low[i][j];
tc[i]+=low[i][j];
id[i][j]=++tot;
comb1(i,n+j,up[i][j]-low[i][j]);
}
}
}
if(flag==true)
{
cout<<"IMPOSSIBLE"<<endl;
cout<<endl;
continue;
}
int cnt=;
p=++tot;
comb1(des,src,inf);
for(int i=src;i<=des;i++)
{
if(tr[i]>tc[i])
{
comb(ss,i,tr[i]-tc[i]);
cnt+=tr[i]-tc[i];
}
if(tc[i]>tr[i])
comb(i,tt,tc[i]-tr[i]);
}
maxflow();
if(ans!=cnt)
{
cout<<"IMPOSSIBLE"<<endl;
cout<<endl;
continue;
}
rest[p]=rest[p^]=;
maxflow();
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
cout<<rest[id[i][j]^]+low[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
return ;
}
算法复习——有源汇上下界可行流(bzoj2396)的更多相关文章
- POJ2396 Budget [有源汇上下界可行流]
POJ2396 Budget 题意:n*m的非负整数矩阵,给出每行每列的和,以及一些约束关系x,y,>=<,val,表示格子(x,y)的值与val的关系,0代表整行/列都有这个关系,求判断 ...
- 有源汇上下界可行流(POJ2396)
题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...
- 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]
题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...
- poj2396 Budget(有源汇上下界可行流)
[题目链接] http://poj.org/problem?id=2396 [题意] 知道一个矩阵的行列和,且知道一些格子的限制条件,问一个可行的方案. [思路] 设行为X点,列为Y点,构图:连边(s ...
- poj2396有源汇上下界可行流
题意:给一些约束条件,要求算能否有可行流,ps:刚开始输入的是每一列和,那么就建一条上下界相同的边,这样满流的时候就一定能保证流量相同了,还有0是该列(行)对另一行每个点都要满足约束条件 解法:先按无 ...
- ZOJ1994有源汇上下界可行流
http://fastvj.rainng.com/contest/236779#problem/G Description: n 行 m 列 给你行和 与 列和 然后有Q个限制,表示特定单元格元素大小 ...
- bzoj 2406 矩阵 —— 有源汇上下界可行流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2406 这题,首先把题目那个式子的绝对值拆成两个限制,就成了网络流的上下界: 有上下界可行流原 ...
- bzoj千题计划158:bzoj2406: 矩阵(有源汇上下界可行流)
http://www.lydsy.com/JudgeOnline/problem.php?id=2406 设矩阵C=A-B 最小化 C 一行或一列和的最大值 整体考虑一行或者一列的和 二分最大值 这样 ...
- bzoj 2406 矩阵——有源汇上下界可行流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2406 二分答案.把 b 的 n 个行作为一排, m 个列作为一排,每行和每列之间连上下界为 ...
随机推荐
- 【Web应用-网络连接】关于 Azure Web 应用 4 分钟空闲连接的限制
Azure Web 应用后台在处理耗时较长的请求时,并且在此期间,客户端和 Azure Web 应用没有数据交互,即 TCP 连接一直处于空闲状态,此种情况超过 4 分钟后,Azure Web 应用会 ...
- JMeter3.2入门使用教程
JMeter3.2入门使用教程 背景说明 1.1. 背景简介 JMeter是Apache软件基金会下的一个开源项目,纯java开发的应用工具,可以作为进行负载和压力测试的工具来使用.从最开始时被设计成 ...
- 如何通过Xcode 5中集成的XCTest框架进行简单的单元测试
XCTest 1.第一个单元测试 XCTest是Xcode 5中自带的测试框架 下面从一个Demo开始.首先用Xcode新建一个工程UnitTestDemo,工程目录结构如下: 可以看到工程下面多了一 ...
- vue使用echarts可视化图形插件
1.安装echarts: cnpm/npm i echarts -S 2.main.js中 import echarts from 'echart' Vue.prototype.$echa ...
- EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap
2018-06-22 09:50:19.488 INFO 20096 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : ...
- 使用Cordova将您的前端JavaScript应用打包成手机原生应用
假设我用JavaScript和HTML开发了一个前端应用,我想把该应用打包成能直接在手机上安装和运行(不通过浏览器)的原生应用,例如像下面这样.对应用的用户来说,他们得到的用户体验和真正的用Andro ...
- CPP-基础:windows api 多线程---互斥量、信号量、临界值、事件区别
http://blog.csdn.net/wangsifu2009/article/details/6728155 四种进程或线程同步互斥的控制方法:1.临界区:通过对多线程的串行化来访问公共资源或一 ...
- 理解GloVe模型(Global vectors for word representation)
理解GloVe模型 概述 模型目标:进行词的向量化表示,使得向量之间尽可能多地蕴含语义和语法的信息.输入:语料库输出:词向量方法概述:首先基于语料库构建词的共现矩阵,然后基于共现矩阵和GloVe模型学 ...
- (49)zabbix事件是什么?事件来源有哪些分类
什么是zabbix 事件 在trigger的文章内,我们已经有用到事件,这个事件要讲概念真心不知道怎么说,就拿trigger事件来说,如果trigger从当前值ok转变为problem,那么我们称之为 ...
- python--前端CSS
一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义了如何显示HTML元素,给HTML设置样式,让他更加美观. 当浏览器读到这个样式表, 他就会按照这个样式来对文档进行 ...