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. Centos7创建CA和申请证书 转自https://www.cnblogs.com/mingzhang/p/8949541.html

    Centos7.3创建CA和申请证书 openssl 的配置文件:/etc/pki/tls/openssl.cnf 重要参数配置路径 dir   = /etc/pki/CA               ...

  2. 1131(★、※)Subway Map

    思路:DFS遍历 #include <iostream> #include <map> #include <vector> #include <cstdio& ...

  3. Android 最简单的MVP案例;

    随手撸个发出来: V:界面层 //界面层需要实现P.View方法,然后重写P.View中的方法:M层给的数据就在这些个方法的参数中: // 还要获取到P.Provide的实例,使用P.Provide去 ...

  4. 团队第三次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-7日晚上9点 召开时长15分钟 任务表格 袁勤 继续学习SpringBoot https://github.com/buaa-2016/phyweb ...

  5. 模拟实现简单ATM功能

  6. html 设置input框的记忆功能(联想内容)

    autocomplete=“on/off” 1.默认情况下,autocomplete的值是on.你可以将其设置为off. 2.autocomplete属性可以放在input 元素上,也可以放在form ...

  7. c# ref与out用法

    class Program { static void Main(string[] args) { //普通 : ; ); Console.WriteLine("/*普通:*/") ...

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

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

  9. spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported

    1.请求设置Content-Type:application/json即可 ajax一般默认:Content-Type: application/x-www-form-urlencoded;chars ...

  10. jsp页面中比较“接收数据”与“页面循环数据”是否相等

    页面中关系运算符: -lt 小于 -le   小于或者等于 -gt 大于 -ge 大于或者等于 -eq 等于 -ne 不等于 判空:<c:if test="${empty count  ...