题目来源:
http://acm.hdu.edu.cn/showproblem.php?pid=3666
题目描述:
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
解题思路:
题目大概就是给你一个矩阵,然后可以问你n个数ai,和m个数bj,使s【i】【j】*ai/bi在l到r之间,好像有别的方法,不过我只会取log,差分约束跑spfa,
L<=m[i][j]*a[i]/b[j]<=U
log(L/m[i][j])<=log(a[i])-log(b[j])<=log(U/m[i][j])
则 :
log(a[i])<=log(b[j])+log(U/m[i][j])
log(b[j])<=log(a[i])+log(m[i][j]/L)
建边就是根据不等式了,
差分约束题型分析,
求最大值,化为a-b<=c,建b到a的边权为c的边,求最短路;
求最小值,化为a-b>=c,建b到a的边权为c的边,求最长路;
存在负环,无解;最短路判负环,最长路判正环
求不出最短路,则有任意解;
还有就是这题正常做会tle,所以玄学的把入队次数改成sqrt(n)就过了;
代码:
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=16*1e4+10;
struct newt
{
int to,next;
double cost;
}e[maxn*2];
int n,m,head[1000],cnt,cs[1000],vis[1000];
void addedge(int u,int v,double w)
{
e[cnt].to=v;
e[cnt].next=head[u];
e[cnt].cost=w;
head[u]=cnt++;
}
double dis[1000];
double l,r;
bool spfa()
{
for(int i=1;i<=n+m;i++)
dis[i]=1e9,vis[i]=0;
//memset(vis,0,sizeof(vis));
queue<int>q;
vis[1]=1;
q.push(1);
cs[1]=1;
dis[1]=0;
int t=(int)sqrt((m+n)*1.0);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
for(int i=head[now];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(dis[v]>dis[now]+e[i].cost)
{
dis[v]=dis[now]+e[i].cost;
if(vis[v])continue;
vis[v]=1;
if(++cs[v]>t)return 0;
q.push(v);
//if(cs[v]>t)return 0;
}
}
}
return 1;
}
int main()
{
while(scanf("%d%d%lf%lf",&n,&m,&l,&r)!=EOF)
{
double c;cnt=0;
memset(cs,0,sizeof(cs));
l=log(l);r=log(r);
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%lf",&c);
c=log(c);
addedge(i,j+n,c-l);
addedge(j+n,i,r-c);
}
if(spfa())puts("YES");
else puts("NO");
}
return 0;
}