You have been given a matrix C N*M, each element E of C N*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.

InputThere 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.

OutputIf 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
 

题意:问是否满足每行乘一个相同的正实数,然后每一列除一个相同的正实数,使得矩阵李每一个数在[L,U]内。

思路:化简后是带系数的不等系组,L*Bj<=X*Ai<=U*Bj,那么取对数即可,把Ai和Bj的系数化为1,然后差分约束即可。

1,是求是否可行,而不是求最大最小。所以用最长路判正环也行,用最短路判负环亦可。因为如过不可行,那么既无最大,也没有最小;而如果有可行解,那么既有最大,又有最小。

2,判环的时候如果按进队次数大于n+m时时退出会超时,所以加了qsrt,虽然我不知道这样是否科学。。。存疑。

3,本题自己限定了正数,方便求解,也避免负时不等号要改变方向。

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const double inf=0x7fffffff;
int Laxt[maxn],Next[maxn<<],To[maxn<<];
int vis[maxn],inq[maxn],cnt,n,m;
double dis[maxn],Len[maxn<<];
void update()
{
cnt=;
memset(Laxt,,sizeof(Laxt));
memset(vis,,sizeof(vis));
memset(inq,,sizeof(inq));
}
void add(int u,int v,double d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt]=d;
}
bool spfa()
{
int times=;
for(int i=;i<=n+m;i++) dis[i]=-inf;
queue<int>q;
q.push(); dis[]=; inq[]=;
while(!q.empty()){
if(times>*(n+m)) return false;
int u=q.front(); q.pop(); inq[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(dis[v]<dis[u]+Len[i]){
dis[v]=dis[u]+Len[i];
if(!inq[v]){
inq[v]=; vis[v]++; q.push(v); times++;
if(vis[v]>sqrt(n+m)) return false;
}
}
}
} return true;
}
int main()
{
int i,j; double x,L,U;
while(~scanf("%d%d%lf%lf",&n,&m,&L,&U)){
update();
L=log10(L);U=log10(U);
for(i=;i<=n;i++)
for(j=;j<=m;j++){
scanf("%lf",&x);
add(n+j,i,L-log10(x));
add(i,n+j,-U+log10(x));
}
for(i=;i<=n+m;i++) add(,i,);
if(spfa()) printf("YES\n");
else printf("NO\n");
} return ;
}
//1,知道要去对数;2,判定时的投机取巧。

HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)的更多相关文章

  1. 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 ...

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

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

  3. hduTHE MATRIX PROBLEM(差分约束)

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

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

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

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

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

  6. HDOJ 1534 Schedule Problem 差分约束

    差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...

  7. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  8. 转载 - 最短路&差分约束题集

    出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548    A strange lift基础最短路(或bfs)★ ...

  9. 鉴于spfa基础上的差分约束算法

    怎么搞?        1. 如果要求最大值      想办法把每个不等式变为标准x-y<=k的形式,然后建立一条从y到x权值为k的边,变得时候注意x-y<k =>x-y<=k ...

随机推荐

  1. Ubuntu -- 下如何查看CPU信息, 包括位数和多核信息

    from: http://hi.baidu.com/sdusoul/blog/item/76f349508f74fb6e843524eb.html 查看当前操作系统内核信息# uname -a Lin ...

  2. HDOJ1071

    The area   拿到题的第一想法,又是一道水题,知道P1.P2.P3三点的坐标,就能够确定抛物线的公式.确定抛物线的公式就能够进行积分,然后就没有然后了.纯粹的数学题. #include< ...

  3. Android 存储(本地存储 SD卡存储 SharedPreference SQLite ContentProvider)

    本文出自:http://blog.csdn.net/dt235201314/article/details/73176149 源码下载欢迎Star(updating):https://github.c ...

  4. 转: WebRTC音视频引擎研究(1)--整体架构分析

    转自: http://blog.csdn.net/temotemo/article/details/7530504   目录(?)[+]   WebRTC技术交流群:234795279 原文地址:ht ...

  5. Qt跨平台的一个例程

    我的同事penk在近期北京的Hackathon展示了一个在多平台的例程. 非常多开发人员对这个挺感兴趣的. 今天我就把这个资源介绍给大家. 这是同一个用Qt写的应用.能够同一时候在Ubuntu Des ...

  6. 【windows socket+UDPserverclient】

    Windows Socket+UDPserverclient      Winsock是 Windows下套接字标准.                    1.UDP socket编程:       ...

  7. Android系统开发(6)——Linux底层输入输出

    一.操作系统的体系结构 计算机是由一堆硬件组成的,操作系统是为了有效的控制这些硬件资源的软件.操作系统除了有效地控制这些硬件资源的分配.并提供计算机执行所须要的功能之外,为了提供程序猿更easy开发软 ...

  8. Spring Cloud(十二):Spring Cloud Zuul 限流详解(附源码)(转)

    前面已经介绍了很多zuul的功能,本篇继续介绍它的另一大功能.在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选 ...

  9. HDU 1418 抱歉 (欧拉公式)

    [题目链接]:pid=1418">click here~~ [题目大意]: 假设平面上有n个点,而且每一个点至少有2条曲线段和它相连,就是说,每条曲线都是封闭的.同一时候,我们规定: ...

  10. python--函数程序分析

    写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作 import os #加载模块 def xiu(a,b,c): #三个接受值的形参 f = open(a,encod ...