zoukankan      html  css  js  c++  java
  • THE MATRIX PROBLEM

    THE MATRIX PROBLEM

    Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 41 Accepted Submission(s): 14
     
    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
     
     
     
    Source
    2010 Asia Regional Harbin
     
    Recommend
    lcy
     
    /*
    题意:给你一个n*m的矩阵,现在问你,存不存在这样的两个序列,a1,a2...an,   b1,b2....bm,使得矩阵每行的元素都乘a序列每
        列的都除以b序列,这个操作之后,矩阵的每个元素都在[L,U]这个区间内。
        
    初步思路:对每个元素与L,U联立不等式,然后按照不等式建边,再用spfa跑一下
    
    #补充:虽然初步思路想的很好但是,还是想不出来怎么才能以i,j为参考进行建边,看了一下题解,L<=num[i][j]*a[i]/b[j]<=U
        可以化简为,L/num[i]<=a[i]/b[i]<=U/num,但是现在中间的除法还是不好处理,经过log之后除法变成减法,就会处理了很多
        log(L/num[i][j])<=log(a[i])-log(b[j])<=log(U/num[i][j]);
    */
    #include<bits/stdc++.h>
    using namespace std;
    /*****************************************************spaf模板*****************************************************/
    const int maxn = 400 + 5;
    const int INF = 1e9 + 7;
    
    typedef struct node{
        int to;
        int next;
        double w;
        node(int a = 0, int b = 0, double c = 0){
            to = a; next = b; w = c;
        }
    }Edge;
    
    int s[maxn * 3];
    double dis[maxn * 3];
    Edge edge[maxn * maxn * 3];
    int tot, head[maxn * maxn * 3];
    int vis[maxn * 3], cnt[maxn * 3];
    
    void add(int u, int v, double w){
        edge[tot] = node(v, head[u], w);
        head[u] = tot++;
    }
    bool spfa(int e){
        int u, v, top = 0;
        for(int i = 0; i <= e; ++i){
            dis[i] = INF;
            vis[i] = 0; cnt[i] = 0;
        }
        s[top++] = 0; vis[0] = 1; dis[0] = 0;
        while(top){
            u = s[--top]; vis[u] = 0;
            if((++cnt[u]) > e) return 0;
            for(int i = head[u]; ~i; i = edge[i].next){
                v = edge[i].to;
                if(dis[v] > dis[u] + edge[i].w){
                    dis[v] = dis[u] + edge[i].w;
                    if(!vis[v]){
                        s[top++] = v;
                        vis[v] = 1;
                    }
                }
            }
        }
        return 1;
    }
    /*****************************************************spaf模板*****************************************************/
    void init(){
        memset(head,-1,sizeof head);
        tot=0;
    }
    int n,m,L,U;
    int num;
    int main(){
        // freopen("in.txt","r",stdin);
        while(~scanf("%d%d%d%d",&n,&m,&L,&U)){
            init();
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    scanf("%d",&num);
                    //log(L/num[i][j])<=log(a[i])-log(b[j])
                    //i-j>=log(L/num[i][j])
                    add(i, j + n, log(1.0 * U / num));
                    //log(U/num[i][j])>=log(a[i])-log(b[j])
                    //i-j<=log(U/num[i][j])
                    
                    add(j + n, i, -log(1.0 * L / num));
                }
            }
            printf(spfa(n+m-1)?"YES
    ":"NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    ExtJs控件属性配置详细
    static void和void区别(转)
    OpenSSL 内存管理分析笔记
    Openssl 之大数运算函数 BN
    python学习:字符串
    python学习:字典
    python学习:购物车程序
    python学习:元组和嵌套
    python学习:列表
    python学习:continue及break使用
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6476401.html
Copyright © 2011-2022 走看看