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\n");
        else printf("NO\n");
    } return 0;
}
//1,知道要去对数;2,判定时的投机取巧。
 

原文地址:https://www.cnblogs.com/hua-dong/p/8438566.html

时间: 2024-10-28 21:05:11

HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)的相关文章

hdu3666 THE MATRIX PROBLEM --- 差分约束

这要是碰上现场赛我得被搞死 从RE到TLE到WA已疯.. 这题建图没有那么直接,通过给出的不等式关系一时想不到怎么建图 所以要对题目给的条件一定程度化简,将不等式两边取对数化简得到Sa-Sb<=c的形式 要注意w取double类型 其次,这题卡时间,根据经验加剪枝: 1.出队次数>sqrt(n)则判断有负环 2.统计总的入队次数,>2n则判断有负环 一般情况下不用这个,因为不严谨 下面两个spfa都是对的,手写队列稍快一点,上面第二个剪枝效果明显 #include<iostream

HDU 3666 THE MATRIX PROBLEM (差分约束)

题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l<=cij*(ai/bj)<=u (1<=i<=n,1<=j<=m)成立. 首先把cij先除到两边去,就变成了l'<=ai/bj<=u',由于差分约束要是的减,怎么变成减法呢?取对数呗,两边取对数得到log(l')<=log(ai)-log(bj)<=l

HDOJ 3666 THE MATRIX PROBLEM 差分约束

根据题意有乘除的关系,为了方便构图,用对数转化乘除关系为加减关系..... THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7486    Accepted Submission(s): 1914 Problem Description You have been given a matrix CN

HDU1534 Schedule Problem 差分约束

囧,还是暴露出了对差分约束理解的不透彻... 一开始根据开始和结束的关系建边,然后建立一个超级源点,连接每一个其他节点,先把这个点入队.本质上相当于把一开始所有的节点都入队了,然后做一遍最长路(最短路,怎么建边的怎么来),相当于把每一个点都作为起点做了一遍最短路,每个点的d取最大的那个. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include &l

HDOJ 1534 Schedule Problem 差分约束

差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1503    Accepted Submission(s): 647 Special Judge Problem Descr

HDU-3666 THE MATRIX PROBLEM

很容易发现约束条件:L<=K[i,j]*A[i]/B[j]<=U 妈呀这可是乘法啊...看起来貌似没法化简... 那么看成对数呢? lg(L)<=lg(K[i,j])+lg(A[i])-lg(B[j])<=lg(U) 这样子就能左移右移了吧=v= 判断是否有负权回路即可... #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #includ

【转载】夜深人静写算法(四)——差分约束

[转载]夜深人静写算法(四) - 差分约束  目录     一.引例       1.一类不等式组的解   二.最短路       1.Dijkstra       2.图的存储       3.链式前向星       4.Dijkstra + 优先队列       5.Bellman-Ford       6.SPFA       7.Floyd-Warshall   三.差分约束        1.数形结合        2.三角不等式        3.解的存在性        4.最大值

hdu 差分约束题集

[HDU]1384 Intervals 基础差分约束★1529 Cashier Employment 神级差分约束★★★★ 1531 King 差分约束★1534 Schedule Problem 差分约束输出一组解★3440 House Man 比较好的差分约束★★3592 World Exhibition 简单★3666 THE MATRIX PROBLEM 中等★★4274 Spy's Work [先处理出欧拉序列,然后就是差分约束了...] [POJ]1201 Intervals1275

Hdu 3666 THE MATRIX PROBLEM(差分约束)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3666 思路:差分约束. 取对数将乘除转化为加减. L<=m[i][j]*a[i]/b[j]<=U log(L/m[i][j])<=log(a[i])-log(b[j])<=log(U/m[i][j]) 则 : log(a[i])<=log(b[j])+log(U/m[i][j]) log(b[j])<=log(a[i])+log(m[i][j]/L) SPFA判