THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5437    Accepted Submission(s): 1372

Problem Description
You have been given a matrix CN*M, each element E of CN*M is positive and no more than 1000, The problem is that if there exist N numbers a1, a2, … an and M numbers b1, b2, …, bm, which satisfies that each elements in row-i multiplied with ai and each elements in column-j divided by bj, after this operation every element in this matrix is between L and U, L indicates the lowerbound and U indicates the upperbound of these elements.
 
Input
There are several test cases. You should process to the end of file.
Each case includes two parts, in part 1, there are four integers in one line, N,M,L,U, indicating the matrix has N rows and M columns, L is the lowerbound and U is the upperbound (1<=N、M<=400,1<=L<=U<=10000). In part 2, there are N lines, each line includes M integers, and they are the elements of the matrix.

 
Output
If there is a solution print "YES", else print "NO".
 
Sample Input
3 3 1 6
2 3 4
8 2 6
5 2 9
 
Sample Output
YES
 
Source
 
Recommend
lcy
 

题目意思就是是否存在ai,bj,使得l<=cij*(ai/bj)<=u (1<=i<=n,1<=j<=m)成立

首先,把cij除到两边:l'<=ai/bj<=u',如果差分约束的话,应该是ai-bj的形式,于是可以取对数

log(l')<=log(ai)-log(bj)<=log(u')

把log(ai)和log(bj)看成两个点ai和bj,化成求最短路的形式:dis[ai]-dis[bj]<=log(u'),dis[bj]-dis[ai]<=-log(l')

然后判负环就行,深搜和广搜都可以

注意,如果spfa队列判负环:

(1)不必判断某个点入队次数大于N,只要判断是否大于sqrt(1.0*N)

(2)或者所有点的入队次数大于T*N,即存在负环,一般T取2

N为所有点的个数

1, SPFA广搜:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath> using namespace std; const int N=; struct Edge{
int to,nxt;
double cap;
}edge[N*N]; int n,m,cnt,head[N];
int vis[N],Count[N];
double dis[N],L,U; void addedge(int cu,int cv,double cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
} int SPFA(){
int limit=(int)sqrt(1.0*(n+m));
queue<int> q;
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
memset(Count,,sizeof(Count));
for(int i=;i<=n+m;i++){
dis[i]=;
q.push(i);
}
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cap){
dis[v]=dis[u]+edge[i].cap;
if(!vis[v]){
vis[v]=;
if(++Count[v]>limit)
return ;
q.push(v);
}
}
}
}
return ;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%lf%lf",&n,&m,&L,&U)){
cnt=;
memset(head,-,sizeof(head));
double x;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%lf",&x);
addedge(j+n,i,log(U/x));
addedge(i,j+n,-log(L/x));
}
if(SPFA())
puts("YES");
else
puts("NO");
}
return ;
}

2, SPFA深搜:(这个更快??)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath> using namespace std; const int N=; struct Edge{
int to,nxt;
double cap;
}edge[N*N]; int n,m,cnt,head[N];
int vis[N],instack[N];
double dis[N],L,U; void addedge(int cu,int cv,double cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
} int SPFA(int u){
if(instack[u])
return ;
instack[u]=;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cap){
dis[v]=dis[u]+edge[i].cap;
if(!SPFA(v))
return ;
}
}
instack[u]=;
return ;
} int solve(){
memset(vis,,sizeof(vis));
memset(instack,,sizeof(instack));
memset(dis,,sizeof(dis));
for(int i=;i<=n+m;i++)
if(!vis[i]){
if(!SPFA(i))
return ;
}
return ;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%lf%lf",&n,&m,&L,&U)){
cnt=;
memset(head,-,sizeof(head));
double x;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%lf",&x);
addedge(j+n,i,log(U/x));
addedge(i,j+n,-log(L/x));
}
if(solve())
puts("YES");
else
puts("NO");
}
return ;
}

HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)的更多相关文章

  1. HDU 3666 THE MATRIX PROBLEM (差分约束)

    题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...

  2. HDU 3666.THE MATRIX PROBLEM 差分约束系统

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)

    You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...

  4. HDU3666-THE MATRIX PROBLEM(差分约束-不等式解得存在性判断 对数转化)

    You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...

  5. hduTHE MATRIX PROBLEM(差分约束)

    题目请戳这里 题目大意:给一个n*m的矩阵,求是否存在这样两个序列:a1,a2...an,b1,b2,...,bm,使得矩阵的第i行乘以ai,第j列除以bj后,矩阵的每一个数都在L和U之间. 题目分析 ...

  6. HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

    题意: 给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存 ...

  7. hdu 3666 THE MATRIX PROBLEM

    差分约束系统. 根据题意,可以写出不等式 L <= (Xij * Ai) / Bj <= U 即 Ai/Bj<=U/Xij和Ai/Bj>=L/Xij 由于差分约束系统是减法.. ...

  8. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

随机推荐

  1. fisher精确检验(fisher’s exat test)和超几何分布

  2. golang包管理工具及环境管理工具;如何下载外网的依赖包

    简介: golang的包管理工具类似于java的maven.python的pip.js的npm,可以实现依赖包的统一管理:有很多:govendor.godep.glide,挑一个自己喜欢的用吧.mac ...

  3. homogeneous clip space and NDC

    CVV  canonical view volume HCS homogeneous clip space NDC nomolized device coordinates pipeline 的 ge ...

  4. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  5. Windows服务器PHPstudy配置安装微擎教程

    此教程只适于无环境的新服务器,有环境请勿安装用电脑上面的远程桌面连接登陆服务器.1,下载微擎安装包,下载PHPstudy,下载V9运行库.安装PHPstudy.安装V9运行库.准备好3个. <i ...

  6. 解决: Connection to https://dl-ssl.google.com refused

    第一步: 在 hosts 中增加以下 地址转义 #Google主页203.208.46.146 www.google.com#这行是为了方便打开Android开发官网 现在好像不FQ也可以打开#74. ...

  7. windows server2012部署Cognos问题小结

    一:数据库配置问题   1:.net Framework3.5问题 window Server2012 安装数据库的时候可能会报缺少.net Framework3.5,需要去服务管理器添加.net F ...

  8. office2007word文档设置多级目录

    office本来不是很难,关键就是经验吧,直入主题. 文档结构图设置了四级,但是目录始终只显示三级,郁闷了好久,网上看的也不靠谱,方法如下: 引用-目录-插入目录 弹出插入目录设置后,修改级别为最大, ...

  9. 用vs2013开发node.js的addon.

        下载node.js的源代码. https://github.com/joyent/node 如果用svn下载,后面加上/trunk,以免把用不着的branches也下载下来,浪费时间. 安装V ...

  10. 【Node.js】初体验之安装和HelloWorld

    听说Node.js是个蛮吊的东东.中午休息时间有限,暂时看了下知道怎么安装和初步使用了. 1.安装: 到Node.js官网下载就可以了,才5M多点,双击后按步骤安装就可以了. 2."Hell ...