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.
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
题解:题目要求我们判断是否存在使矩阵的num[i][j]*a[n-i]/b[m-j]后为L到U之间的数值的两组数 即为L=<num[i][j]*a[i]/b[j]<=U,我们可以变为 log(b[i])-log(a[i])<=log(num[i][j])-log(L) , log(a[i])-log(b[i])<=log(U)-log(num[i][j])两个;即想到差分约束解得存在性(判断是否含有负环,若有,则无解,没有,则有解);
参考代码如下:
#include<bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 1e3;
const int INF = 1e9;
int c, vis[maxn], cnt[maxn], n, m, l, r;
double dis[maxn];
struct node
{
int to;
double w;
node(){}
node(int tt, double ww) : to(tt), w(ww){}
};
vector<node> v[maxn];
void spfa()
{
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < maxn; i++) dis[i] = INF;
queue<int> q; q.push(0);
vis[0] = 1; dis[0] = 0; cnt[0] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = 0; i < v[u].size(); i++)
{
int to = v[u][i].to;
double w = v[u][i].w;
if(dis[u] + w < dis[to])
{
dis[to] = dis[u] + w;
if(!vis[to])
{
vis[to] = 1;
if(++cnt[to]>sqrt(n+m))
{
printf("NO
");
return;
}
q.push(to);
}
}
}
}
printf("YES
");
return ;
}
int main()
{
while(~scanf("%d%d%d%d", &n,&m,&l,&r))
{
for(int i=0;i<maxn;i++) v[i].clear();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d", &c);
v[n+j].push_back(node(i, log(r)-log(c)));
v[i].push_back(node(n+j,log(c)-log(l)));
}
for(int i = 1; i <= n+m; i++) v[0].push_back(node(i, 0));
spfa();
}
return 0;
}