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直接构造减号就可以了
这样再跑一边最长路,看有没有负环,以及无法到达的点。
判断负环的方法是一个点入队次数>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");
}
}
}