HDU 3666 THE MATRIX PROBLEM(差分约束)

THE MATRIX PROBLEM

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


 

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

 

题意:

给你一个n*m的矩阵,让第i行的元素都乘以ai,第j列的元素都除以bj,使得矩阵的所有元素都在[L,R]之间

问你能否构造出这样的a[],b[]

解析:

这道题想了一天,想不出来怎么把里面的系数去掉,结果看了题解,完全没必要,用log直接构造减号就可以了

L\leq x_{i,j}*a_{i}/b_{j}\leq R

L/x_{i,j}\leq a_{i}/b_{j}\leq R/x_{i,j}

log(L/x_{i,j})\leq log(a_{i})-log(b_{j})\leq log(R/x_{i,j})

\begin{cases} & \text log(a_{i})-log(b_{j}) \geq log(L/x_{i,j}) & \text log(b_{j})-log(a_{i})\geq log(x_{i,j}/R) \end{cases}

 

这样再跑一边最长路,看有没有负环,以及无法到达的点。

判断负环的方法是一个点入队次数>sqrt(n+m)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include<cmath>
using namespace std;
const int MAX = 4e2 + 10;
#define INF 0x7f7f7f7f
struct node
{
	int v;
	double val;
};

vector<node> edges[MAX*2];
double d[MAX * 2];
int in_times[MAX * 2];
double A[MAX][MAX];
queue<int> mq;
bool vis[MAX * 2];


bool SPFA(int s, double limt)
{
	while (!mq.empty()) mq.pop();
	mq.push(s);
	d[s] = 0;
	vis[s] = true;
	while (!mq.empty())
	{
		int u = mq.front();
		mq.pop();

		if (in_times[u] > limt)
		{
			return false;
		}
		vis[u] = false;
		for (int i = 0; i < edges[u].size(); i++)
		{
			node tmp = edges[u][i];

			if (d[u] + tmp.val > d[tmp.v])
			{
				d[tmp.v] = d[u] + tmp.val;
				if (!vis[tmp.v])
				{
					mq.push(tmp.v);
					in_times[tmp.v]++;
					vis[tmp.v] = true;
				}

			}
		}
	}
	return true;

}

int main()
{
	int n, m;
	double L, R;
	while (scanf("%d%d%lf%lf", &n, &m, &L, &R)!=EOF)
	{
		//a1..an : 1...n
		//b1..bm : n+1...n+m
		for (int i = 0; i <= n + m + 1; i++) edges[i].clear();
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				scanf("%lf", &A[i][j]);
				int a = i;
				int b = j + n;
				edges[b].emplace_back(node{ a,log(L) - log(A[i][j]) });
				edges[a].emplace_back(node{ b,log(A[i][j]) - log(R) });
			}
		}

		for (int i = 1; i <= n + m; i++)
		{
			d[i] = -INF;
			in_times[i] = 0;
			vis[i] = false;
		}

		if (SPFA(1, std::sqrt(n + m)))
		{
			/*for (int i = 1; i <= n; i++)
			{
				for (int j = 1; j <= m; j++)
				{
					printf("%.5lf ", A[i][j] * exp(d[i]-d[j + n]));
				}
				printf("\n");
			}*/
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值