HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)

Posted ---学习ing---

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)相关的知识,希望对你有一定的参考价值。

 

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.

InputThere 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. 

OutputIf 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
 

题意:问是否满足每行乘一个相同的正实数,然后每一列除一个相同的正实数,使得矩阵李每一个数在[L,U]内。

思路:化简后是带系数的不等系组,L*Bj<=X*Ai<=U*Bj,那么取对数即可,把Ai和Bj的系数化为1,然后差分约束即可。

1,是求是否可行,而不是求最大最小。所以用最长路判正环也行,用最短路判负环亦可。因为如过不可行,那么既无最大,也没有最小;而如果有可行解,那么既有最大,又有最小。

2,判环的时候如果按进队次数大于n+m时时退出会超时,所以加了qsrt,虽然我不知道这样是否科学。。。存疑。

3,本题自己限定了正数,方便求解。

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=500010;
const double inf=0x7fffffff;
int Laxt[maxn],Next[maxn<<1],To[maxn<<1];
int vis[maxn],inq[maxn],cnt,n,m;
double dis[maxn],Len[maxn<<1];
void update()
{
    cnt=0;
    memset(Laxt,0,sizeof(Laxt));
    memset(vis,0,sizeof(vis));
    memset(inq,0,sizeof(inq));
}
void add(int u,int v,double d)
{
    Next[++cnt]=Laxt[u];
    Laxt[u]=cnt;
    To[cnt]=v;
    Len[cnt]=d;
}
bool spfa()
{
    int times=0;
    for(int i=1;i<=n+m;i++)  dis[i]=-inf;
    queue<int>q;
    q.push(0); dis[0]=0; inq[0]=1;
    while(!q.empty()){
        if(times>10*(n+m)) return false;
        int u=q.front(); q.pop(); inq[u]=0;
        for(int i=Laxt[u];i;i=Next[i]){
            int v=To[i];
            if(dis[v]<dis[u]+Len[i]){
                dis[v]=dis[u]+Len[i];
                if(!inq[v]){
                   inq[v]=1; vis[v]++; q.push(v); times++;
                   if(vis[v]>sqrt(n+m)) return false;
                }
            }
        }
    } return true;
}
int main()
{
    int i,j; double x,L,U;
    while(~scanf("%d%d%lf%lf",&n,&m,&L,&U)){
        update();
        L=log10(L);U=log10(U);
        for(i=1;i<=n;i++) 
         for(j=1;j<=m;j++){
            scanf("%lf",&x);
            add(n+j,i,L-log10(x));
            add(i,n+j,-U+log10(x));
        }
        for(i=1;i<=n+m;i++) add(0,i,0);
        if(spfa()) printf("YES\n");
        else printf("NO\n");
    } return 0;
}
//1,知道要去对数;2,判定时的投机取巧。
 

 

以上是关于HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)的主要内容,如果未能解决你的问题,请参考以下文章

HDU4355-Party All the Time-三分

hdu1029 Ignatius and the Princess IV

[POJ3666] Making the Grade

Harry Potter and the Hide Story(hdu3988)

Making the Grade POJ - 3666

[poj3666]Making the Grade