THE MATRIX PROBLEM(差分约束)

C - THE MATRIX PROBLEM
Time Limit:2000MS Memory Limit:32768KB
64bit IO Format:%I64d & %I64u

Description

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
题目大意:有一个矩阵C,有N行,M列,问是否存在这么两个数列An,Bm,使对任意i,j满足Cij*Ai和Cij/Bj都在区间[L,U]之间。
分析:由题意得L<=Cij*Ai<=U,L<=Cij/Bj<=U,因为差分约束的条件都是加减关系,所以此处应用对数来进行变换,得
log(L)<=log(Cij*Ai)<=log(U),log(L)<=log(Cij/Bj)<=log(U),
再变换得
log(L)<=log(Cij)+log(Ai)<=log(U),log(L)<=log(Cij)-log(Bj)<=log(U),
两式相加得
2log(L)<=2log(Cij)+log(Ai)-log(Bj)<=2log(U),
最终得到
log(Bj)-log(Ai)<=2log(Cij)-2log(L)和log(Ai)-log(Bj)<=2log(U)-log(Cij)
两个式子
由此再转换为最短路的状态转移方程,建图,求是否存在最短路即可,因为当i=j时,Ai与Bj是不一样的,所以此处将j加上n,求1到n+m的最短路,还有用spfa判环时,用常规条件入队次数大于n+m会超时,改为大于sqrt(n+m)就可以过,此处有些卡数据,还需要思考一下
代码:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define inf 0x3f3f3f3f
#define maxn 805
using namespace std;
struct node
{
    int en;
    double len;
    int next;
};
int num;
int head[maxn];
node E[maxn*maxn];
bool vis[maxn];
double dis[maxn];
int n,m;
int numm[maxn];
double c;
void init()
{
     num=0;
     memset(head,-1,sizeof(head));
     memset(numm,0,sizeof(numm));
}
void add(int st,int en,double len)
{
     E[num].en=en;
     E[num].len=len;
     E[num].next=head[st];
     head[st]=num++;
}
bool spfa()
{
     memset(vis,false,sizeof(vis));
     for(int i=1;i<=n+m;i++)
     dis[i]=inf;
     queue<int> q;
     vis[1]=true;
     dis[1]=0.0;
     q.push(1);
     numm[1]++;
     while(!q.empty())
     {
          int xx=q.front();
          q.pop();
          vis[xx]=false;
          for(int i=head[xx];i!=-1;i=E[i].next)
          {
               int ed=E[i].en;
               if(dis[ed]>dis[xx]+E[i].len)
               {
                   dis[ed]=dis[xx]+E[i].len;
                   if(!vis[ed])
                   {
                       q.push(ed);
                       numm[ed]++;
                       if(numm[ed]>(int)sqrt(n+m))
                       return false;
                       vis[ed]=true;
                   }
               }
          }
     }
     if(dis[n+m]<inf)
     return true;
     else
     return false;//1到m+n不存在最短路
}
int main()
{
    int i,j,k;
    double l,u;
    while(scanf("%d%d%lld%lld",&n,&m,&l,&u)!=EOF)
    {
        init();
        for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
             scanf("%lld",&c);
             add(i,j+n,2.0*log(c)-2.0*log(l));
             add(j+n,i,2.0*log(u)-2.0*log(c));
        }
        if(spfa())
        printf("YES\n");
        else
        printf("NO\n");
    }
    getchar();
    getchar();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值