天天看點

HDU 3666 THE MATRIX PROBLEM(差分限制)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直接構造減号就可以了

HDU 3666 THE MATRIX PROBLEM(差分限制)THE MATRIX PROBLEM
HDU 3666 THE MATRIX PROBLEM(差分限制)THE MATRIX PROBLEM
HDU 3666 THE MATRIX PROBLEM(差分限制)THE MATRIX PROBLEM
HDU 3666 THE MATRIX PROBLEM(差分限制)THE MATRIX PROBLEM

這樣再跑一邊最長路,看有沒有負環,以及無法到達的點。

判斷負環的方法是一個點入隊次數>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");
		}
	}

}