HDU 3666 THE MATRIX PROBLEM(差分约束)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37025443/article/details/83744253

THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9438    Accepted Submission(s): 2423


 

Problem Description

You have been given a matrix CN*M, each element E of CN*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的矩阵,让第i行的元素都乘以ai,第j列的元素都除以bj,使得矩阵的所有元素都在[L,R]之间

问你能否构造出这样的a[],b[]

解析:

这道题想了一天,想不出来怎么把里面的系数去掉,结果看了题解,完全没必要,用log直接构造减号就可以了

L\leq x_{i,j}*a_{i}/b_{j}\leq R

L/x_{i,j}\leq a_{i}/b_{j}\leq R/x_{i,j}

log(L/x_{i,j})\leq log(a_{i})-log(b_{j})\leq log(R/x_{i,j})

\begin{cases} & \text log(a_{i})-log(b_{j})\leq log(R/x_{i,j}) & \text log(b_{j})-log(a_{i})\leq log(x_{i,j}/L) \end{cases}

然后是构造源点与所有的点连一条权值为0的边。

a_{1}-x_{0}\leq 0

a2-x0\leq 0

......

b1-x0\leq 0

....

这样一开始把d[0]=0,其实是无所谓的,因为这样所有的值求出来的就是负数,但是ai/bj只要符号相同,结果都是一样的。

其实对于源点只连一个点也是可以的,因为这道题是并不是求值,而是能不能跑通,所以只要连一个点(譬如说ai)进去跑spfa,一样是可以A的,只是最后d[]可能不是最小值。

这样再跑一边最短路,看有没有负环,以及无法到达的点。

判断负环的方法是一个点入队次数>sqrt(n+m)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <bitset>
#include <cmath>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int MAXN = 1e3+10;
const double eps=1e-8;

double mp[500][500];

typedef struct node
{
    int v;
    double w;
    int nxt;
}node;

int head[MAXN];
node edge[MAXN*MAXN];
int vis[MAXN];
int n,cnt;
double d[MAXN];
int ji[MAXN];
int up;

void addedge(int u,int v,double w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].nxt=head[u];
    head[u]=cnt++;
}
queue<int> q;

int spfa(int src)
{
    while(q.size()) q.pop();
    for(int i=0;i<=n;i++) d[i]=INF,vis[i]=0,ji[i]=0;
    d[src]=0;
    q.push(src);
    ji[0]++;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        if(ji[u]>up) return 0;
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].nxt)
        {
            node x=edge[i];
            double  tmp=d[u]+x.w;
            if(d[x.v]>tmp+eps)
            {
                d[x.v]=tmp;
                if(!vis[x.v])
                {
                    vis[x.v]=1;
                    ji[x.v]++;
                    q.push(x.v);
                }
            }
        }
    }
    return 1;
}


int main()
{
   int m;
   double L,R;
   while(scanf("%d%d%lf%lf",&n,&m,&L,&R)!=EOF)
   {
   memset(head,-1,sizeof(head));

    cnt=0;
   for(int i=1;i<=n;i++)
   {
       for(int j=1;j<=m;j++)
       {
           scanf("%lf",&mp[i][j]);
           addedge(i,n+j,log(mp[i][j]/L));
           addedge(n+j,i,log(R/mp[i][j]));
       }
   }
   n+=m;
   up=sqrt(n);
   for(int i=1;i<=n;i++)
   {
       addedge(0,i,0);
   }
   //addedge(0,1,0);

   int flag=spfa(0);
   /*for(int i=1;i<=n;i++)
   {
       if(d[i]==INF)
       {
           flag=0;
           break;
       }
   }*/
   if(!flag) printf("NO\n");
   else printf("YES\n");
   }


}

猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/83744253