THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7819    Accepted Submission(s): 2019

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   |   We have carefully selected several similar problems for you:  3665 3669 3667 3664 3663 

刚开始用差分约束写的,我去,超时到最后!!!!后来找优化算法,slf跟lll都还没看,看到了深搜的SPFA

差分约束代码,别人提交的就能过,我的就超时,搞不懂,这一定不会是人品问题,对的,一定不会是!!!!!

超时代码:(应该是oj编译器问题或者就是AC的标准提高了)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 1000
#define MAXM 500000+10
#define INF 0x3f3f3f
int dis[MAXN],vis[MAXN],used[MAXN],m,n;
int head[MAXN],cnt;
double map[MAXN][MAXN];
double L,U;
struct node
{
int u,v;
double val;
int next;
}edge[MAXM];
void init()
{
memset(head,-1,sizeof(head));
memset(map,0,sizeof(map));
cnt=0;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void getmap()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%lf",&map[i][j]);
add(j+n,i,log(U/map[i][j]));
add(i,j+n,-log(L/map[i][j]));
}
}
for(int i=1;i<=n+m;i++)
add(0,i,0);
}
void SPFA()
{
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
memset(dis,INF,sizeof(dis));
queue<int>q;
q.push(0);
dis[0]=0;
used[0]++;
vis[0]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[E.u]+E.val)
{
dis[E.v]=dis[E.u]+E.val;
if(!vis[E.v])
{
vis[E.v]=1;
used[E.v]++;
if(used[E.v]>(int)sqrt(1.0*n+m))
{
cout<<"NO"<<endl;
return ;
}
q.push(E.v);
}
}
}
}
cout<<"YES"<<endl;
}
int main()
{
while(scanf("%d%d%lf%lf",&n,&m,&L,&U)!=EOF)
{
init();
getmap();
SPFA();
}
return 0;
}

SPFA深搜版


#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stack>
#include<queue>
using namespace std;
const int MAX=805;
struct node
{
int v,next;
double c;
}g[MAX*MAX];
int adj[MAX];
int n,m,e;
double dis[MAX],l,u;
bool vis[MAX],inStack[MAX];
inline void add(int u,int v,double c)
{
g[e].v=v; g[e].c=c; g[e].next=adj[u]; adj[u]=e++;
}
bool spfa(int u)
{
int i,v;
if(inStack[u])
return false;
inStack[u]=true;
vis[u]=true;
for(i=adj[u];i!=-1;i=g[i].next)
{
v=g[i].v;
if(dis[v]>dis[u]+g[i].c)
{
dis[v]=dis[u]+g[i].c;
if(!spfa(v))
{
return false;
}
}
}
inStack[u]=false;
return true;
}
bool ok()
{
int i,u,v,cnt=0;
memset(vis,0,sizeof(vis));
memset(inStack,0,sizeof(inStack));
for(i=0;i<=n+m;i++)
{
dis[i]=0;
}
for(i=1;i<=n+m;i++)
{
if(!vis[i])
{
if(!spfa(i))
{
return false;
}
}
}
return true;
}
int main()
{
int i,j;
double t;
while(scanf("%d%d %lf %lf",&n,&m,&l,&u)!=EOF)
{
e=0;
memset(adj,-1,sizeof(adj));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%lf",&t);
add(j+n,i,log(u/t));
add(i,j+n,-log(l/t));
}
}
if(ok())
puts("YES");
else
puts("NO");
}
return 0;
}

hdoj--3666--THE MATRIX PROBLEM(差分约束+SPFA深搜)的更多相关文章

  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. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  7. HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)

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

  8. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  9. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

随机推荐

  1. 希尔加密算法(湖南师范大学第六届大学生计算机程序设计竞赛)hnuoj11552

    解密 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 2, Accept ...

  2. linux下使用convert命令修改图片分辨率【转】

    本文转载自:http://blog.csdn.net/mybelief321/article/details/9969949 Convert的resize子命令应该是在ImageMagick中使用较多 ...

  3. oracle故障处理之删除大表空间hang住

    背景 数据库分区表数据越来越大,需要对过期话的数据进行迁移,以及大的分区表需要进行数据的清理和删除,达到释放磁盘空间的目的. 问题说明 环境:linux 6.X 数据库:oracle 11.2.0.4 ...

  4. 项目中解决实际问题的代码片段-javascript方法,Vue方法(长期更新)

    总结项目用到的一些处理方法,用来解决数据处理的一些实际问题,所有方法都可以放在一个公共工具方法里面,实现不限ES5,ES6还有些Vue处理的方法. 都是项目中来的,有代码跟图片展示,长期更新. 1.获 ...

  5. MVC bundle配置文件模板

    bundle文件放在应用根目录,命名为  bundle.config 内容模板 <?xml version="1.0" encoding="utf-8"? ...

  6. Java算法——数组

    * 已知一个数组int[98],该数组里面存储了0~99共100个数字中的98个,数字不重复,请用算法算出0~99中缺少的2个数字是哪两个? * 要求:数组自己用程序生成,数值介于0~99,相互之间不 ...

  7. Android 的永久登陆 与注销登陆

    一.永久登陆 sharedprefrence 存储 userID  以及 password private String FILE = "saveUserNamePwd";//用于 ...

  8. [oracle] 组织架构退格显示 connect by

    1. 按组织架构关系退格显示 create or replace view v_vieworg asselect --v.OBJID,v.OBJNAMElevel as levelid, lpad(' ...

  9. UNP学习笔记3——基本UDP套接字编程

    1 概述 TCP和UDP网络编程存在一些本质的差异,主要是由于传输层的差别:UDP是无连接的不可靠的数据报协议,而TCP是面向连接的字节流协议. 下图是典型的UDP客户端和服务器之间的通信流程.客户不 ...

  10. CorelDRAW X6最新注册激活机制

    最近购买CorelDRAW X6的小伙伴可能对如何注册激活软件存在疑惑,下面小编一步步教您如何快速激活CorelDRAW X6. CorelDRAW X6最新注册机制如下: 1.关注“Corel服务中 ...