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*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

/* ***********************************************
Author        :CKboss
Created Time  :2015年07月29日 星期三 20时55分16秒
File Name     :HDOJ3666.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=1000;
const double eps=1e-8;

int n,m;
double L,R;

struct Edge
{
	int to,next;
	double cost;
}edge[maxn*maxn*2];

int Adj[maxn],Size;

void init()
{
	memset(Adj,-1,sizeof(Adj)); Size=0;
}

void Add_Edge(int u,int v,double c)
{
	edge[Size].to=v;
	edge[Size].next=Adj[u];
	edge[Size].cost=c;
	Adj[u]=Size++;
}

double dist[maxn];
int cQ[maxn];
bool inQ[maxn];

bool spfa(int rt)
{
	for(int i=0;i<n+m+10;i++)
		dist[i]=1e40;
    memset(cQ,0,sizeof(cQ));
    memset(inQ,false,sizeof(inQ));

    dist[rt]=0;
    queue<int> q;
    inQ[rt]=true;q.push(rt); cQ[rt]=1;

    while(!q.empty())
    {
        int u=q.front();q.pop();

        for(int i=Adj[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].cost)
            {
                dist[v]=dist[u]+edge[i].cost;
                if(!inQ[v])
                {
                    inQ[v]=true;
                    cQ[v]++;
                    if(cQ[v]>=sqrt(n+m)) return false;
                    q.push(v);
                }
            }
        }
        inQ[u]=false;
    }
    return true;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	while(scanf("%d%d%lf%lf",&n,&m,&L,&R)!=EOF)
	{
		init();

		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				double x;
				scanf("%lf",&x);
				int a=i,b=n+j;
				Add_Edge(b,a,log(R/x));
				Add_Edge(a,b,-log(L/x));
			}
		}

		for(int i=1;i<=n+m;i++)
			Add_Edge(0,i,0);

		bool fg=spfa(0);

		if(fg) puts("YES");
		else puts("NO");
	}

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 18:08:42

HDOJ 3666 THE MATRIX PROBLEM 差分约束的相关文章

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

hdu3666 THE MATRIX PROBLEM --- 差分约束

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

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

HDU1534 Schedule Problem 差分约束

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

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判

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

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

差分约束算法总结

来自 https://blog.csdn.net/my_sunshine26/article/details/72849441 差分约束系统 一.概念 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统. 二.引例 给定n个变量和m个不等式,每个不等式的形式为 x[i] - x[j] <= a[k] (0 <= i, j < n, 0 <= k < m, a[k]已知),求 x[i] - x[j]

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