Hdu3666 the matrix prolem

Title Link

analysis:
From the questions, we can get:
L<=C[i][j]∗a[i]b[j]<=RL<=C[i][j]∗a[i]b[j]<=R

We need to determine the aa and bb arrays (n+mn+m in total)
So we should get a formula about aa or bb
b[j]<=a[i]∗C[i][j]Lb[j]<=a[i]∗C[i][j]L
a[i]<=b[j]∗RC[i][j]a[i]<=b[j]∗RC[i][j]

The forms of these two inequalities are a little familiar
In the difference constraint, the relationship between variables is satisfied: Xi < = XJ + zxi < = XJ + Z
But all we get is multiplication and division
However, we can convert multiplication and division into addition and subtraction

Take the logarithm of all the numbers, and then convert the above formula to:

lg(b[j])<=lg(a[i])+[lg(C[i][j])−lg(L)]lg(b[j])<=lg(a[i])+[lg(C[i][j])−lg(L)]
lg(a[i])<=lg(b[j])+[lg(R)−lg(C[i][j])]lg(a[i])<=lg(b[j])+[lg(R)−lg(C[i][j])]

The shortest path of this problem needs to be completed with floating-point numbers

tip

There are several test cases.

Multiplication and division - > logarithm - > addition and subtraction

I've been stuck with the constant since I wrote it
queue in STL used at the beginning
If I don'T think it's good, I changed it to handwritten, but it's still T
Look at the student sister's blog and say that you can quit as long as you reach sqrt (n + m) and sqrt (n + m) repeatedly
With this condition, it's still T

At last, I found that the way array is small...

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>

using namespace std;

const double INF=1e9;
const double eps=1e-7;
const int N=805;
struct node{
    int y,nxt;
    double v;
};
node way[400000];
int n,m,st[N],tot=0,cnt[N],Q[N],tt;
double L,R,dis[N];
bool in[N];

int dcmp(double x)
{
    if (fabs(x)<eps) return 0;
    else if (x>0) return 1;
    else return -1;
}

void add(int u,int w,double z)
{
    tot++;
    way[tot].y=w;way[tot].v=z;way[tot].nxt=st[u];st[u]=tot;
}

bool Bellman()
{
    queue<int> Q;
    for (int i=1;i<=n;i++)
    {
        dis[i]=0.0; in[i]=1; cnt[i]=0; Q.push(i);
    }
    while (!Q.empty())
    {
        int now=Q.front(); Q.pop();
        in[now]=0;
        for (int i=st[now];i;i=way[i].nxt)
            if (dcmp(dis[way[i].y]-dis[now]-way[i].v)>0)
            {
                dis[way[i].y]=dis[now]+way[i].v;
                if (!in[way[i].y])
                {
                    in[way[i].y]=1;
                    if (++cnt[way[i].y]>sqrt(n)) return 0;
                    Q.push(way[i].y);
                }
            }
    }
    return 1;
}

int main()
{
    while (scanf("%d%d%lf%lf",&n,&m,&L,&R)!=EOF)
    {
        memset(st,0,sizeof(st)); tot=0;

        L=log(L);
        R=log(R);
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
            {
                double c;
                scanf("%lf",&c);
                c=log(c);
                add(i,j+n,c-L);
                add(j+n,i,R-c);
            }
        n+=m;
        if (Bellman()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

Posted by Hyperjase on Sun, 03 May 2020 22:42:18 -0700