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 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
 
 
 
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\n":"NO\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/6476401.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值