HDUoj 3666 THE MATRIX PROBLEM 差分约束

来源:互联网 发布:mac照片上传icloud 编辑:程序博客网 时间:2024/06/05 22:45

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 100010typedef 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
原创粉丝点击