You have been given a matrix C N*M, each element E of C N*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.
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.
If there is a solution print “YES”, else print “NO”
给出一个 N∗M N ∗ M 的矩阵 C C ,要求构造两个序列 a1,a2,...,an a 1 , a 2 , . . . , a n 和 b1,b2,...,bm b 1 , b 2 , . . . , b m
矩阵的第 i i 行元素乘上 ai a i ,矩阵的第 j j 列元素除以 bj b j ,最终矩阵的每个元素 Cij∈[L,U] C i j ∈ [ L , U ]
若可以构造成功,则输出”YES”,否则输出”NO”
依题意
#include
using namespace std;
const double inf = 1e9;
const int maxn = 550;
struct node
{
int to;
double w;
node (int i=0,double j=0)
{
to = i, w = j;
}
};
int n,m,l,u,uptimes,cnt[maxn<<1];
double d[maxn<<1];
bool vis[maxn<<1];
vector g[maxn<<1];
bool spfa()
{
queue q;
q.push(0);
vis[0] = 1;
d[0] = 0;
while (!q.empty())
{
int u = q.front();
for (int i=0;iif (d[v] > d[u]+dis)
{
d[v] = d[u]+dis;
if (!vis[v])
{
vis[v] = 1;
q.push(v);
if (++cnt[v] > uptimes) return false;
}
}
}
q.pop();
vis[u] = 0;
}
return true;
}
int main()
{
while (scanf("%d %d %d %d",&n,&m,&l,&u)!=EOF)
{
uptimes = sqrt(n+m);
for (int i=0;i0;
d[i] = inf;
}
int x;
for (int i=0;ifor (int j=0;j"%d",&x);
g[j+n].push_back(node(i,log((double)u/x)));
g[i].push_back(node(j+n,log((double)x/l)));
}
if (spfa()) printf("YES\n");
else
printf("NO\n");
}
return 0;
}