zoukankan      html  css  js  c++  java
  • 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
    ");
            else printf("NO
    ");
        } return 0;
    }
    //1,知道要去对数;2,判定时的投机取巧。
     
  • 相关阅读:
    2017-3-13 SQL server 表连接
    2017-3-13 SQL server 函数(聚合函数,数学函数,字符串函数,转换函数,时间日期函数)
    2017-3-10 SQL server T-sql语句 高级查询
    2017-3-10 SQL server 数据库 T--SQL语句
    layer框架使用的问题汇总
    pulic——功能性(自己写完测试的)list转树
    bootstrap的datetimepicker使用(1.将默认的英文设置为中文2.选择日月年的时候记录之前的操作)
    临时笔记0318
    最简单的多线程代码,一句话就可以使用
    scrollview滑动到某区域执行某种方法
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8438566.html
Copyright © 2011-2022 走看看