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)<=log(u‘)。

然后把ai, bj看成是两个点,那两个是权值,就可以差分约束了,但是。。这个题太坑了,会TLE,必须要判断好结束条件,就是访问次数超过sqrt(m+n),

就结束,如果不开根号,就会一直TLE。。。。有没有天理了。。。。

析:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int maxn = 800 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int head[maxn], to[maxn*maxn/2], Next[maxn*maxn/2], cnt;
double w[maxn*maxn/2], l, u, d[maxn];

void addedge(int u, int v, double c){
    to[cnt] = v;
    w[cnt] = c;
    Next[cnt] = head[u];
    head[u] = cnt++;
}
int vis[maxn], num[maxn];

bool spfa(){
    memset(vis, 0, sizeof(vis));
    memset(num, 0, sizeof(num));
    fill(d, d+n+m+1, inf);
    queue<int> q;
    vis[0] = 1;  d[0] = 0;  num[0] = 1;
    q.push(0);
    int limit = sqrt(m+n+0.5);//不开根号,想AC?都到没有。

    while(!q.empty()){
        int u = q.front();  q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = Next[i]){
            int v = to[i];
            double c = w[i];
            if(!vis[v] && d[v] > d[u] + c){
                if(++num[v] > limit)  return false;
                d[v] = d[u] + c;
                q.push(v);
                vis[v] = 1;
            }
        }
    }
    return true;
}

int main(){
    while(scanf("%d %d %lf %lf", &n, &m, &l, &u) == 4){
        memset(head, -1, sizeof(head));
        cnt = 0;
        double ll = log(l);
        double uu = log(u);
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < m; ++j){
                double x;
                scanf("%lf", &x);
                x = log(x);
                addedge(i, j+n, x-ll);
                addedge(j+n, i, uu-x);
            }
        }
        if(spfa())  puts("YES");
        else puts("NO");
    }
    return 0;
}

  

时间: 2024-11-07 14:12:33

HDU 3666 THE MATRIX PROBLEM (差分约束)的相关文章

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

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(差分约束)

题目地址: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判

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 e

HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

题意:给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存在输出yes,否则no. 思路:能够得到的是一对不等式,那么可以用最短路来解决差分约束系统.但是a[i][j]*ai/bj<=U是除的,得提前变成减的才行.可以用log来解决,先不管a[i][j],logai-logbj<=U不就行了?可以得到: (1)logai - logbj<=U/a[i

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 3592 World Exhibition (差分约束,spfa,水)

题意: 有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多少?(若INF则输出-2,若冲突则输出-1,否则输出距离) 思路: 建图时都将约束转成a-b<=c的标准形式,然后建一条b->a的边,权为c.然后求最短路,注意最短路跑出来的结果却是最远的合法距离,而不是最短距离.本题无需添加辅助边,只要到达不了n,则距离为INF,输出-2,若有负环,那肯定是冲突

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