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. Coursera课程《大家的编程》(Python入门)中课程目录

    Getting Started with Python Getting Started with Python is the first course in the specialization Py ...

  2. 【BZOJ】【2741】【FOTILE模拟赛】L

    可持久化Trie+分块 神题……Orz zyf & lyd 首先我们先将整个序列搞个前缀异或和,那么某一段的异或和,就变成了两个数的异或和,所以我们就将询问[某个区间中最大的区间异或和]改变成 ...

  3. java中需要用equals来判断两个字符串值是否相等

    在C++中,两个字符串比较的代码可以为: (string1==string2) 但在java中,这个代码即使在两个字符串完全相同的情况下也会返回false Java中必须使用string1.equal ...

  4. jQuery多文件下载

    文件下载是一个Web中非常常用的功能,不过你是做内部管理系统还是做面向公众的互联网公司都会遇到这个问题,对于下载一般有点实际开发经验的都会自己解决,上周弄了一下多文件下载,业务场景就是一条数据详细信息 ...

  5. win7+iis7+asp+.net+php环境配置

    一.我们先来配置一下iis: f 1. 点击[開始]->[控制面板]->点击[程序和功能]进入下一步 2. 然后,在左側点击[打开或关闭Windows功能].然后会跳出来一个框 3. 開始 ...

  6. 【MySQL】MySQL-主从复制-集群方案-数据一致性问题解决方案 && MySQL备份的各种姿势

    1.写性能如何保证:分库分表 2.读性能如何保证:主从结构,实时备份 3.一致性问题怎么解决: 3.1.微博案例:Redis缓存,热数据查询走Redis,主从的延迟通过Redis消除 3.2.支付宝的 ...

  7. 【转】php里面也可以使用协程

    原文链接:http://blog.51cto.com/chinalx1/2089327 http://nikic.github.io/2012/12/22/Cooperative-multitaski ...

  8. HBase数据迁移至Hive

    背景:需要将HBase中表xyz(列簇cf1,列val)迁移至Hive 1. 建立Hive和HBase的映射关系     1.1 运行hive shell进入hive命令行模式,运行如下脚本 CREA ...

  9. Linux内核二层数据包接收流程

    本文主要讲解了Linux内核二层数据包接收流程,使用的内核的版本是2.6.32.27 为了方便理解,本文采用整体流程图加伪代码的方式从内核高层面上梳理了二层数据包接收的流程,希望可以对大家有所帮助.阅 ...

  10. Yahoo团队总结的关于网站性能优化的经验(转)

    英文原文:http://developer.yahoo.com/performance/rules.html 中文原文:http://www.ha97.com/2710.html 1.尽量减少HTTP ...