HDU 3666 THE MATRIX PROBLEM (建图+差分约束+剪枝优化)

16 篇文章 1 订阅
9 篇文章 0 订阅


THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8563    Accepted Submission(s): 2215


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
 

Recommend
lcy
 

题目大意:

给你一个n*m的矩阵,现在有一个长度为n的序列a,一个长度为m的序列b,让你把这个矩阵第i行的所有元素都乘以a[i],把第j列的元素都除以b[j],问你存不存在这样的两个序列a,b,使得经过这些操作之后的矩阵每个元素都在[L, U]之间


题目意思就是是否存在ai,bj,使得l<=cij*(ai/bj)<=u (1<=i<=n,1<=j<=m)成立

首先,把cij除到两边:l'<=ai/bj<=u',如果差分约束的话,应该是ai-bj的形式,于是可以取对数

log(l')<=log(ai)-log(bj)<=log(u')

 把log(ai)和log(bj)看成两个点ai和bj,化成求最短路的形式:dis[ai]-dis[bj]<=log(u'),dis[bj]-dis[ai]<=-log(l')


如果spfa队列判负环:

(1)不必判断某个点入队次数大于N,只要判断是否大于sqrt(1.0*N)

(2)或者所有点的入队次数大于T*N,即存在负环,一般T取2

N为所有点的个数

这也不知道对不对...比赛里一直tle的话,可以试试。。。



总结:以后有大于某数小于某数的时候都要想想差分约束, 想办法构造减法, 除法可以构造减法, 加法也应该可以,这样乘法也可以了。。总之有大于小于, 最长最短, 能否成功, 是否存在的,都想想差分约束~

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 1e3;
const int INF = 1e9;
int c[maxn][maxn], book[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(book, 0, sizeof(book));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < maxn; i++) dis[i] = INF;
    queue<int> q;
    q.push(0);
    book[0] = 1;
    dis[0] = 0;
    cnt[0] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        book[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(!book[to])
                {
                    book[to] = 1;
                    if(++cnt[to] > sqrt(n+m))
                    {
                        printf("NO\n");
                        return;
                    }
                    q.push(to);
                }
            }
        }
    }
    printf("YES\n");
    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[i][j]);
                v[n+j].push_back(node(i, log(r)-log(c[i][j])));
                v[i].push_back(node(n+j,log(c[i][j])-log(l)));
            }
        for(int i = 1; i <= n+m; i++)
            v[0].push_back(node(i, 0));
        spfa();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值