THE MATRIX PROBLEM

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

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
 
题意:是否存在数组a,b使得l/G[i][j]<=a[i]/b[j]<=u/G[i][j]
思路:乘除变加减取log,加减边乘除去指数。问题变成log2(l)-log2(G[i][j])<=log2(a[i])-log2(b[j])<=log2(u)-log(G[i][j]),这是差分约束系统是否有解,即最短路求解,是否存在负圈。spfa算法,当所有入队列的次数>2*n,即存在负圈,或者每个点入队列的次数>n,其实>sqrt(n+1)就可以了。
dist[i]表示起点s到i的最短距离,对于<u,v>,则dist[u]+w>=dist[v]。所以dist[v]-diat[u]<=w; 
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int> P;
const int N=,M=1e6;
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
const double INF=;
struct edge
{
int from,to;
double w;
int next;
};
int n,m;
edge es[M];
int cut,head[N];
double dist[N];
void init()
{
cut=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,double w)
{
///cout<<u<<" ** "<<v<<" ** "<<w<<endl;
cut++;
es[cut].from=u,es[cut].to=v;
es[cut].w=w;
es[cut].next=head[u];
head[u]=cut;
}
bool spfa()
{
int cou=;
queue<int>q;
bool inq[N];
for(int i=; i<=n+m+; i++) dist[i]=inf,inq[i]=false;
dist[]=;
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=false;
if(++cou>*(n+m)) return false;
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(dist[e.to]>dist[e.from]+e.w)
{
dist[e.to]=dist[e.from]+e.w;
///cout<<e.from<<" * "<<e.to<<" * "<<dist[e.to]<<endl;
if(!inq[e.to]) q.push(e.to),inq[e.to]=true;
}
}
}
return true;
}
int main()
{
double l,u;
while(scanf("%d%d%lf%lf",&n,&m,&l,&u)!=EOF)
{
init();
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
double g;
scanf("%lf",&g);
addedge(i,n+j,log2(g)-log2(l));
addedge(n+j,i,log2(u)-log2(g));
}
}
if(spfa()) 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. HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

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

  4. hdu 3666 THE MATRIX PROBLEM

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

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

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

  7. hduTHE MATRIX PROBLEM(差分约束)

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

  8. ZOJ 1455 Schedule Problem(差分约束系统)

    // 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...

  9. 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. <realsense D400>同步采集深度图和彩色图

    利用深度相机采集深度图和彩色图会面临一个问题,如何实现同步采集数据? 以下是我搜集到的两点方法: 1)高翔博士提到他的orbslam2教程有这么一步工作,具体目录为 example/RGBD/. (等 ...

  2. mysql 插入更新判断 ON DUPLICATE KEY UPDATE 和 REPLACE INTO

    平时我们在设计数据库表的时候总会设计 unique 或者 给表加上 primary key 的限制条件.此时 插入数据的时候 ,经常会有这样的情况:我们想向数据库插入一条记录: 若数据表中存在以相同主 ...

  3. Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和导出. 该 ...

  4. Android SurfaceView及TextureView对比

    SurfaceView是什么? 它继承自类View,因此它本质上是一个View.但与普通View不同的是,它有自己的Surface.有自己的Surface,在WMS中有对应的WindowState,在 ...

  5. 洛谷P1443马的遍历

    传送 这是个广搜,思路和普通的迷宫题差不多,但我卡了3遍,为什么呢? 因为输出格式 题目要求左对齐,宽度为5输出,在此说一下如何控制宽度. 下面的m都为要求的宽度 int 类型: printf: %m ...

  6. RHEL7调图形化

    RHEL7调图形化 作者:Eric 微信:loveoracle11g 1.将默认的运行级别修改为"多用户,无图模式" [root@server ~]# ln -sf /lib/sy ...

  7. 使用Oracle DBLink进行数据库之间对象的访问操作

    Oracle中自带了DBLink功能,它的作用是将多个oracle数据库逻辑上看成一个数据库,也就是说在一个数据库中可以操作另一个数据库中的对象,例如我们新建了一个数据database1,我们需要操作 ...

  8. first*php*self*

    要完成一个简单的php程序,首先就是要分析一下要完成所需要实现哪些功能,以及页面的整体布局,以及会涉及到的一部分数据库的函数以及SQL语句. 今天自己简要做了一个php程序,拿到手后就开始创建所能用到 ...

  9. Java 身份证判断性别获取年龄

    import com.alibaba.fastjson.JSON; import org.junit.Test; import java.text.SimpleDateFormat; import j ...

  10. Thrift 的五种工作模式

    一.thrift 共有5中工作模式,分成阻塞和非阻塞: 阻塞:TSimpleServer.TThreadPoolServer 非阻塞:TNonblockingServer.THsHaServer.TT ...