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 ;
}

最新文章

  1. SAP CRM 使用Javascript触发SAP Server Event
  2. 不需要了解任何底层知识,就可以汉化!Let`s go!!!
  3. REST Web 服务介绍
  4. Android开发手记(30) 触摸及手势操作
  5. OC对象:封装、继承、多态的使用举例一
  6. gradle构建依赖
  7. iOS的扩展类,扩展属性
  8. css3属性——border-radius用法
  9. makefile里有哪些target?
  10. Python学习--Python变量类型
  11. 将 Azure 文件共享用于 Windows VM
  12. Dubbo的使用入门
  13. [日常] Go语言圣经--Map习题
  14. python 搭建http服务器和ftp服务器
  15. 7、redis之使用spring集成commons-pool来操作常见数据类型
  16. OAF SubTabLayoutBean隐藏子控件
  17. quartz 的简单使用
  18. python 分词计算文档TF-IDF值并排序
  19. 前端PHP入门-004-数据类型,特别需要注意字符串
  20. SQL Server更改排序规则的实现过程

热门文章

  1. 硬件负载均衡F5和软负责均衡Nginx
  2. 在JSP中应用JavaBean
  3. 寻路DEMO
  4. libuv之介绍
  5. App优化 StrictMode 严格模式
  6. CSS结构和层叠
  7. JAVA输出指定目录下的子目录和子文件
  8. java读取某个目录是否有新增文件(轮询)
  9. Git Submodule使用完整教程
  10. 【Docker】基于docker+etcd+confd + haproxy构建高可用、自发现的web服务