题意:给一些约束条件,要求算能否有可行流,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有源汇上下界可行流的更多相关文章

  1. POJ2396 Budget [有源汇上下界可行流]

    POJ2396 Budget 题意:n*m的非负整数矩阵,给出每行每列的和,以及一些约束关系x,y,>=<,val,表示格子(x,y)的值与val的关系,0代表整行/列都有这个关系,求判断 ...

  2. 有源汇上下界可行流(POJ2396)

    题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...

  3. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

  4. poj2396 Budget(有源汇上下界可行流)

    [题目链接] http://poj.org/problem?id=2396 [题意] 知道一个矩阵的行列和,且知道一些格子的限制条件,问一个可行的方案. [思路] 设行为X点,列为Y点,构图:连边(s ...

  5. 算法复习——有源汇上下界可行流(bzoj2396)

    题目: Description We are supposed to make a budget proposal for this multi-site competition. The budge ...

  6. ZOJ1994有源汇上下界可行流

    http://fastvj.rainng.com/contest/236779#problem/G Description: n 行 m 列 给你行和 与 列和 然后有Q个限制,表示特定单元格元素大小 ...

  7. bzoj 2406 矩阵 —— 有源汇上下界可行流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2406 这题,首先把题目那个式子的绝对值拆成两个限制,就成了网络流的上下界: 有上下界可行流原 ...

  8. bzoj千题计划158:bzoj2406: 矩阵(有源汇上下界可行流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2406 设矩阵C=A-B 最小化 C 一行或一列和的最大值 整体考虑一行或者一列的和 二分最大值 这样 ...

  9. poj2396 Budget&&ZOJ1994 Budget[有源汇上下界可行流]

    Budget Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge We are supposed to make ...

随机推荐

  1. Qt for Android 启动短暂的黑屏或白屏问题如何解决?

    解决方法一: 使用透明主题 点击项目 -> 在 构建设置 里面找到 Build Android APK 栏目,点击 create templates 创建一个 AndroidManifest.x ...

  2. 为什么一定要调用 setlocale 呢? 因为在 C/C++ 语言标准中定义了其运行时的字符集环境为 "C" ,也就是 ASCII 字符集的一个子集。使用setlocal改变整个应用程序的字符集编码方式(wcstombs使用前要设置 setlocale (LC_ALL, "chs"); )

    setlocale 配置地域化信息. 语法: string setlocale(string category, string locale); 返回值: 字符串 函数种类: 操作系统与环境   内容 ...

  3. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

  4. 剑指offer 面试5题

    面试5题: 题目:请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 方法一: # -*- co ...

  5. Linux软件包管理 RMP包

    RPM 包的安装虽然很方便和快捷,但是依赖性实在是很麻烦,尤其是库文件依赖,还要去 rpmfind 网站査找库文件到底属于哪个 RPM 包,从而导致 RPM 包的安装非常烦琐.那么,有没有可以自动解决 ...

  6. jQuery 中让我误解的那些方法

    至今我都不能说把 jQuery 中的方法在实践中都用了一遍, 一部分是用不到,一部分则是我未能体会它的魅力, 所以今天就来收录一下,那些从我们之间溜走的美丽. $.fn.add() 一开始对它的理解就 ...

  7. linux ip别名和辅助ip地址

    转:https://blog.csdn.net/xiewen99/article/details/54729112?utm_source=itdadao&utm_medium=referral ...

  8. Linux静默安装weblogic

    本实验安装weblogic10系列版本 #创建weblogic用户组. [root@admin /]# groupadd weblogic[root@admin /]# useradd -g webl ...

  9. 编写自已的第一个MapReduce程序

    从进入系统学习到现在,貌似我们还没有真正开始动手写程序,估计有些立志成为Hadoop攻城狮的小伙伴们已经有些急了.环境已经搭好,小讲也有些按捺不住了.今天,小讲就和大家一起来动手编写我们的第一个Map ...

  10. php数组函数-array_flip()

    array_flip()函数返回一个反转后的数组,如果同一个值出现多次,则最 后一个键名作为它的值,所有其他的键名将丢失. 如果原数组中的值得数据类型不是字符串或整数,函数将报错. array_fli ...