HDOJ 3666 THE MATRIX PROBLEM

THE MATRIX PROBLEM

TimeLimit: 4000/2000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 8529    Accepted Submission(s): 2206

Problem Description

You have been given a matrix CN*M, each element E of CN*M ispositive 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, afterthis operation every element in this matrix is between L and U, L indicates thelowerbound 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 lowerboundand 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 elementsof 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

 


差分约束总结:传送门

题意:给出一个矩阵问是否存在n个数a1, a2, … an以及m个数b1, b2, …, bm,使矩阵的第i行的元素乘上ai,第j列的元素乘上bj,使得操作后矩阵中的每个元素都在【L,R】范围内。


思路:这算是隐藏比较深的差分约束了,我们可以进行以下推导:

根据题意        L<=mp[i][j]*a[i]/b[j]<=R

移项                    L/mp[i][j]<=a[i]/b[j]<=R/mp[i][j]

两边取对数         log(L/mp[i][j])<=log(a[i]/b[j])<=log(R/mp[i][j])

即                       log(L/mp[i][j])<=log(a[i])-log(b[j])<=log(R/mp[i][j])

建立不等式         log(b[j])-log(a[i])<=-log(L/mp[i][j]) 

                          log(a[i])-log(b[j])<=log(R/mp[i][j])


于是就可以建边了,建边后跑个最短路看看解是否存在即可,由于这样建边没有起点,故我们需要自己加上一个超级源点,让它与所有点相连,权值为0,还有一种方法是SPFA开始时把所有点都扔进队列中进行松弛操作。

Attention:由于这道题时间卡得比较紧,判断是否存在负环时需要优化下,判断条件为入队次数>sqrt(n+m).

  

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
typedef long long ll;
const int maxn= 805;
const int mod = 10007;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)

double dis[maxn];
int vis[maxn];
int num[maxn];
int head[maxn];
int n,m,cnt;

struct node
{
    int v,next;
    double w;
} e[maxn*(maxn+1)];

void add(int u,int v,double w)
{
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}


void init()
{
    cnt=0;
    mst(head,-1);
}


int spfa(int s)
{
    mst(vis,0);
    mst(num,0);
    mst(dis,0x3f);
    queue<int>q;
    vis[s]=1;
    dis[s]=0;
    num[s]++;
    q.push(s);
    while(q.size())
    {
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head[now]; i!=-1; i=e[i].next)
        {
            int v=e[i].v;
            double w=e[i].w;
            if(dis[v]>dis[now]+w)
            {
                dis[v]=dis[now]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                    num[v]++;
                    if(num[v]>sqrt(n+m))
                        return 0;
                }
            }
        }
    }
    return 1;
}

int main()
{
    double L,U;
    double x;
    while(~scanf("%d%d%lf%lf",&n,&m,&L,&U))
    {
        init();
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            scanf("%lf",&x);
            add(n+j,i,log(U/x));
            add(i,n+j,-log(L/x));
        }
        for(int i=1;i<=n+m;i++)
            add(0,i,0);
        if(spfa(0))
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

                



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值