HDUoj 3666 THE MATRIX PROBLEM 差分约束

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

题意:给定一个n*m的矩阵,找2组数列a[n],b[m],使l<=Map[i][j]*a[i]/b[j]<=u。能找到输出“YES”,否则输出“NO”。

由这个不等式得出这是一道差分约束,但又不满足像标准差分约束形式那种标准的d[en]-dis[st] >= val。所以需要转换,利用数学中log进行转换。ps(math.h中log表示log(num)以e 为底的对数)

对不等式两边取log得
log(l) <= log(Map[i][j])+log(a[i])-long(b[j]) <= log(u);

移项并转换成<=符号得
log(a[i])-long(b[j]) <= log(u) - log(Map[i][j]);
log(b[j])-long(a[i]) <= log(Map[i][j]) - log(l);

右边为已知值,a[i]和b[j]可以转换为点i->j的一条边,因为i和j有重叠,所以可以写成i->j+n的连边。这样图就建好了。

用spfa来跑,没负环输出YES,否则输出NO。标准的spfa。
判断出队次数用sqrt((n+m)*1.0)。因为n+m会超时。至于为什么,我不知道,看的别人的,火山哥也这么讲的。

Map和a,b不用写出来,只是中间变量,方便理解

CODE

#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"string.h"
#include"math.h"
#include"stdlib.h"
#include"queue"
#include"math.h"
#define inf 0x3fffffff
#define maxn 100010
typedef long long LL;
using namespace std;

struct node   ///边
{
    int en;
    double val;
    int next;
}edge[maxn*10];
int n,m; ///n行m列
int len;  ///边数
double l,u,ll,uu;  ///ll=log(l),uu = log(u)
int head[2*maxn];  ///头结点
bool vis[2*maxn];   ///spfa标记
double dis[2*maxn];   ///spfa用
int flag[2*maxn];   ///spafa进队次数

void init()   ///初始化
{
    len = 0;
    memset(head,-1,sizeof(head));
    for(int i = 0;i <= n*m;i++)
    {
       // head[i] = -1;
        dis[i] = inf;
        flag[i] = 0;
        vis[i] = false;
    }
}

void add(int st,int en,double val)  ///邻接表加边
{
    edge[len].en = en;
    edge[len].val = val;
    edge[len].next = head[st];
    head[st] = len++;
}

bool spfa()   ///spfa模版
{
    dis[1] = 0;
    queue<int>q;
    q.push(1);
    flag[1]++;
    vis[1] = true;
    int nm = (int)sqrt((n+m)*1.0);   ///入队次数sqrt(n+m)次
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u];i != -1;i = edge[i].next)
        {
            node e = edge[i];
            if(dis[e.en] > dis[u]+e.val)
            {
                dis[e.en] = dis[u]+e.val;
                if(!vis[e.en])
                {
                    vis[e.en] = true;
                    flag[e.en]++;
                    if(flag[e.en] > nm)
                        return false;
                    q.push(e.en);
                }
            }
        }
    }
    return true;
}
int main(void)
{
    while(scanf("%d%d%lf%lf",&n,&m,&l,&u) !=EOF)
    {
        init();
        ll = log(l);  ///方便计算
        uu = log(u);  ///+1
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= m;j++)
            {
                double t;
                scanf("%lf",&t);
                t = log(t);
                add(j+n,i,uu-t);   ///log(b[j])-long(a[i])  <= log(Map[i][j]) - log(l);
                add(i,j+n,t-ll);   ///log(a[i])-long(b[j])  <= log(u) - log(Map[i][j]);
            }
        }
        if(spfa())   ///输出
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值