1. 程式人生 > >HDU 3666 THE MATRIX PROBLEM(差分約束)

HDU 3666 THE MATRIX PROBLEM(差分約束)

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");
   }


}