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

    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
     题解:题目要求我们判断是否存在使矩阵的num[i][j]*a[n-i]/b[m-j]后为L到U之间的数值的两组数 即为L=<num[i][j]*a[i]/b[j]<=U,我们可以变为 log(b[i])-log(a[i])<=log(num[i][j])-log(L) , log(a[i])-log(b[i])<=log(U)-log(num[i][j])两个;即想到差分约束解得存在性(判断是否含有负环,若有,则无解,没有,则有解);

    参考代码如下:

    ​
    #include<bits/stdc++.h>
    using namespace std;
    using namespace std;
    const int maxn = 1e3;
    const int INF = 1e9;
    int c, vis[maxn], cnt[maxn], n, m, l, r;
    double dis[maxn];
    struct node
    {
        int to;
        double w;
        node(){}
        node(int tt, double ww) : to(tt), w(ww){}
    };
    vector<node> v[maxn];
    void spfa()
    {
        memset(vis, 0, sizeof(vis));
        memset(cnt, 0, sizeof(cnt));
        for(int i = 0; i < maxn; i++) dis[i] = INF;
        queue<int> q; q.push(0);
        vis[0] = 1; dis[0] = 0; cnt[0] = 1;
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
            vis[u] = 0;
            for(int i = 0; i < v[u].size(); i++)
            {
                int to = v[u][i].to;
                double w = v[u][i].w;
                if(dis[u] + w < dis[to])
                {
                    dis[to] = dis[u] + w;
                    if(!vis[to])
                    {
                        vis[to] = 1;
                        if(++cnt[to]>sqrt(n+m))
                        {
                            printf("NO
    ");
                            return;
                        }
                        q.push(to);
                    }
                }
            }
        }
        printf("YES
    ");
        return ;
    }
    int main()
    {
        while(~scanf("%d%d%d%d", &n,&m,&l,&r))
        {
            for(int i=0;i<maxn;i++) v[i].clear();
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                {
                    scanf("%d", &c);
                    v[n+j].push_back(node(i, log(r)-log(c)));
                    v[i].push_back(node(n+j,log(c)-log(l)));
                }
            for(int i = 1; i <= n+m; i++) v[0].push_back(node(i, 0));
            spfa();
        }
        return 0;
    }
    
    ​
  • 相关阅读:
    OpenSSL: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    for循环用了那么多次,但你真的了解它么?
    使用git克隆github上的项目失败,报错error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054
    idea修改svn地址
    Eureka服务注册中心错误:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
    Tensorflow学习资源
    编程工具使用技巧
    博客链接
    python学习笔记
  • 原文地址:https://www.cnblogs.com/csushl/p/9386771.html
Copyright © 2020-2023  润新知