poj2396有源汇上下界可行流
题意:给一些约束条件,要求算能否有可行流,ps:刚开始输入的是每一列和,那么就建一条上下界相同的边,这样满流的时候就一定能保证流量相同了,还有0是该列(行)对另一行每个点都要满足约束条件
解法:先按无源汇上下界可行流建边,然后添加一条从t到s的容量为inf的边,从超级源到超级汇跑一边最大流,看流量是不是等于新加边的流量和,注意这题有可能输入的数据会有锚段,那么我们需要特判一下是否有矛盾出现
还要注意的一点是:我刚开始是用string+cin读入的字符,但是出现了问题,导致我代码下面的那组数据不能运行,改成scanf却能运行了,为什么单个字符就不能用string读入呢,还是说有其他的格式问题?
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int from,to,Next,c,low;
}e[maxn<<];
int cnt,head[N];
int in[N],out[N];
int dis[N];
int minn[N][N],maxx[N][N];
void add(int u,int v,int c,int low)
{
// cout<<u<<" "<<v<<" "<<c<<" "<<low<<endl;
out[u]+=low;
in[v]+=low;
e[cnt].from=u;
e[cnt].to=v;
e[cnt].c=c;
e[cnt].low=low;
e[cnt].Next=head[u];
head[u]=cnt++;
e[cnt].from=v;
e[cnt].to=u;
e[cnt].c=;
e[cnt].low=low;
e[cnt].Next=head[v];
head[v]=cnt++;
}
void init(int n,int m)
{
cnt=;
memset(head,-,sizeof head);
memset(in,,sizeof in);
memset(out,,sizeof out);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
minn[i][j]=,maxx[i][j]=inf;
}
bool bfs(int s,int t)
{
memset(dis,-,sizeof dis);
dis[s]=;
queue<int>q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
if(x==t)return ;
for(int i=head[x];~i;i=e[i].Next)
{
int te=e[i].to;
if(dis[te]==-&&e[i].c>)
{
dis[te]=dis[x]+;
q.push(te);
}
}
}
return ;
}
int dfs(int x,int mx,int t)
{
if(x==t)return mx;
int flow=;
for(int i=head[x];~i;i=e[i].Next)
{
int te=e[i].to,f;
if(dis[te]==dis[x]+&&e[i].c>&&(f=dfs(te,min(mx-flow,e[i].c),t)))
{
e[i].c-=f;
e[i^].c+=f;
flow+=f;
}
}
if(!flow)dis[x]=-;
return flow;
}
int maxflow(int s,int t)
{
int ans=,f;
while(bfs(s,t))
{
while((f=dfs(s,inf,t)))ans+=f;
}
return ans;
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
init(n,m);
int s=n+m+,t=n+m+,sum1=,sum2=;
bool can=;
for(int i=;i<=n;i++)
{
int a;
scanf("%d",&a);
sum1+=a;
add(s,i,,a);
if(a<)can=;
}
for(int i=;i<=m;i++)
{
int a;
scanf("%d",&a);
sum2+=a;
add(i+n,t,,a);
if(a<)can=;
}
int res;
scanf("%d",&res);
while(res--)
{
int a,b,c;char str;
scanf("%d %d %c %d",&a,&b,&str,&c);
if(str=='<'&&c<)can=;
if(str=='=')
{
if(a==&&b==)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
minn[i][j]=max(minn[i][j],c);
maxx[i][j]=min(maxx[i][j],c);
}
}
else if(a!=&&b==)
{
for(int j=;j<=m;j++)
{
minn[a][j]=max(minn[a][j],c);
maxx[a][j]=min(maxx[a][j],c);
}
}
else if(a==&&b!=)
{
for(int i=;i<=n;i++)
{
minn[i][b]=max(minn[i][b],c);
maxx[i][b]=min(maxx[i][b],c);
}
}
else if(a!=&&b!=)
{
minn[a][b]=max(minn[a][b],c);
maxx[a][b]=min(maxx[a][b],c);
}
}
else if(str=='>')
{
if(a==&&b==)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
minn[i][j]=max(minn[i][j],c+);
}
}
else if(a!=&&b==)
{
for(int j=;j<=m;j++)
{
minn[a][j]=max(minn[a][j],c+);
}
}
else if(a==&&b!=)
{
for(int i=;i<=n;i++)
{
minn[i][b]=max(minn[i][b],c+);
}
}
else if(a!=&&b!=)
{
minn[a][b]=max(minn[a][b],c+);
}
}
else
{
if(a==&&b==)
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
maxx[i][j]=min(maxx[i][j],c-);
}
}
else if(a!=&&b==)
{
for(int j=;j<=m;j++)
{
maxx[a][j]=min(maxx[a][j],c-);
}
}
else if(a==&&b!=)
{
for(int i=;i<=n;i++)
{
maxx[i][b]=min(maxx[i][b],c-);
}
}
else if(a!=&&b!=)
{
maxx[a][b]=min(maxx[a][b],c-);
}
}
}
int be=cnt;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
// cout<<maxx[i][j]<<" "<<minn[i][j]<<endl;
if(maxx[i][j]<minn[i][j])can=;
add(i,j+n,maxx[i][j]-minn[i][j],minn[i][j]);
}
}
int en=cnt-;
add(t,s,inf,);
int ss=n+m+,tt=n+m+,sum3=;
for(int i=;i<=n+m+;i++)
{
if(in[i]>out[i])add(ss,i,in[i]-out[i],),sum3+=in[i]-out[i];
else add(i,tt,out[i]-in[i],);
}
if(sum1!=sum2||!can||sum3!=maxflow(ss,tt))puts("IMPOSSIBLE");
else
{
can=;
for(int i=be;i<=en;i+=)
{
if(e[i^].c+e[i].low<)
{
can=;
break;
}
}
if(can==)puts("IMPOSSIBLE");
else
{
int co=;
for(int i=be;i<=en;i+=)
{
printf("%d",e[i^].c+e[i].low);
co++;
if(co!=m)printf("%c",' ');
else puts(""),co=;
}
}
}
puts("");
}
return ;
}
/********************
179 20
10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000
89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500 89500
5
130 0 > 307
0 0 < 366
0 0 > 329
0 0 < 341
0 0 < 324
********************/
poj2396有源汇上下界可行流的更多相关文章
- 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 ...
- 算法复习——有源汇上下界可行流(bzoj2396)
题目: Description We are supposed to make a budget proposal for this multi-site competition. The budge ...
- 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 一行或一列和的最大值 整体考虑一行或者一列的和 二分最大值 这样 ...
- poj2396 Budget&&ZOJ1994 Budget[有源汇上下界可行流]
Budget Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge We are supposed to make ...
随机推荐
- diff工具
Beyond Compare 4 可以diff文件夹.单个文件.
- 编译java-cef
javacef即java Chromium Embedded Framework,其功能是通过在java应用中嵌入谷歌浏览器内核Chromium. 编译java-cef的过程可参考以下文档及视频: h ...
- shadow批量破解
john有个参数可以设置破解时间,比如破解5秒则设置:--max-run-time=5,可以利用这个参数批量破解 for i in *;do (echo $i>>out;john --ma ...
- Python 学习之旅
流程图 第一章 Python简介 第二章 Python基础 第三章 流程控制 第四章 字符编码 第五章 文件处理 第六章 函数 第七章 模块与包 第八章 面向对象 第九章 异常处理 第十章 ...
- Python-openpyxl操作
from openpyxl import Workbook from openpyxl import load_workbook # 加载workbook,注意,openpyxl只支持xlsx格式 w ...
- 阿里云公网IP不能使用
1.开通专用网络 2.在ECS的安全组 创建 专用网络 3.配置规则 4.快速创建规则,增加自己需要入网的端口号,授权对象写:0.0.0.0/0
- python常用模块——random模块
参考博客:http://www.360doc.com/content/14/0430/11/16044571_373443266.shtml 今天突然想起python该怎么生成随机数?查了一下,贴出实 ...
- GIS学习和开发的在线资源
1.OpenGIS Consortium标准,http://www.opengeospatial.org.著名的OGC标准是每个GIS开发者最后都不得不学习的,或深或浅. 2.SharpMap,Pro ...
- vue(组件、路由)懒加载
const Login = resolve => require(['@/components/Login'], resolve) //就不用import了 Vue.use(Router) le ...
- DNS 域名解析原理
域名解析过程 1.在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用这个IP地址映射,完成域名解析. 2.如果hosts里没有这个 ...