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 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
题解:题目要求我们判断是否存在使矩阵的num[i][j]*a[n-i]/b[m-j]后为L到U之间的数值的两组数 即为L=<num[i][j]*a[i]/b[j]<=U,我们可以变为 log(b[i])-log(a[i])<=log(num[i][j])-log(L) , log(a[i])-log(b[i])<=log(U)-log(num[i][j])两个;即想到差分约束解得存在性(判断是否含有负环,若有,则无解,没有,则有解);
参考代码如下:
#include<bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 1e3;
const int INF = 1e9;
int c, vis[maxn], cnt[maxn], n, m, l, r;
double dis[maxn];
struct node
{
int to;
double w;
node(){}
node(int tt, double ww) : to(tt), w(ww){}
};
vector<node> v[maxn];
void spfa()
{
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < maxn; i++) dis[i] = INF;
queue<int> q; q.push(0);
vis[0] = 1; dis[0] = 0; cnt[0] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = 0; i < v[u].size(); i++)
{
int to = v[u][i].to;
double w = v[u][i].w;
if(dis[u] + w < dis[to])
{
dis[to] = dis[u] + w;
if(!vis[to])
{
vis[to] = 1;
if(++cnt[to]>sqrt(n+m))
{
printf("NO\n");
return;
}
q.push(to);
}
}
}
}
printf("YES\n");
return ;
}
int main()
{
while(~scanf("%d%d%d%d", &n,&m,&l,&r))
{
for(int i=0;i<maxn;i++) v[i].clear();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d", &c);
v[n+j].push_back(node(i, log(r)-log(c)));
v[i].push_back(node(n+j,log(c)-log(l)));
}
for(int i = 1; i <= n+m; i++) v[0].push_back(node(i, 0));
spfa();
}
return 0;
}
HDU3666-THE MATRIX PROBLEM(差分约束-不等式解得存在性判断 对数转化)的更多相关文章
- 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 p ...
- HDU 3666 THE MATRIX PROBLEM (差分约束)
题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...
- hduTHE MATRIX PROBLEM(差分约束)
题目请戳这里 题目大意:给一个n*m的矩阵,求是否存在这样两个序列:a1,a2...an,b1,b2,...,bm,使得矩阵的第i行乘以ai,第j列除以bj后,矩阵的每一个数都在L和U之间. 题目分析 ...
- HDU 3666.THE MATRIX PROBLEM 差分约束系统
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]
题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- hdu 1534 Schedule Problem (差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 差分约束详解&&洛谷SCOI2011糖果题解
差分约束系统: 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统(system of difference const ...
- HDOJ 1534 Schedule Problem 差分约束
差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...
随机推荐
- 连接xshell 时 连不上的问题
最近这一周由于自己的xshell突然连接不到虚拟机,在网上找了很多种方法也没能解决,以至于自己在学习很多知识的时候都没能很好的去验证,去尝试.最后在求助大佬的时候终于将xshell重新连接到了虚拟 ...
- tomcat 日志(2)
一.Log4j在Tomcat中的配置说明(tomcat6) 学习Java中,从简单的开始.如果需要文中提到的文件可以找我要. http://www.apache.org/dist/tomcat/tom ...
- raid10配置流程(5块磁盘)
1.添加5块磁盘 (1)先添加一块磁盘 (2)在此基础上,再添加4块磁盘 2.使用mdadm命令创建RAID10,名称为“/dev/md0” -C 代表创建操作 -v 显示创建过程 ...
- PHP的两种选择防止sql注入
1.使用PDO: $stmt = $pdo->prepare('SELECT * FROM user WHERE name = :name'); $stmt->execute(array( ...
- opencv 5 图像转换(1 边缘检测)
边缘检测 一般步骤 canny算子 步骤 canny函数 彩色canny #include<opencv2/opencv.hpp> #include<opencv2/highgui/ ...
- linux 6.5操作系统建立
VM上redhat enterprise linux6 ---> 使用仅主机模式网络连接 开始安装: ——>是否检查镜像:skip ——>选择语言: 中文/英文 ——>存储设备 ...
- Python函数的默认参数的设计【原创】
在Python教程里,针对默认参数,给了一个“重要警告”的例子: def f(a, L=[]): L.append(a) return L print(f(1)) print(f(2)) print( ...
- 图解 Spring:HTTP 请求的处理流程与机制【1】
2003 年,老兵哥初到中兴开始研究生实习,Spring 就是那年诞生的,2004 年 3 月发布了 1.0 版本,到现在已经超过 15 年了.从单体式分层架构到云原生微服务架构,它稳坐在 JAVA ...
- 使用laravel快速构建vuepress管理器
使用laravel快速构建vuepress管理器 介绍 刚刚学了下laravel感觉很方便,最近也在用vuepress做个人博客,但是感觉每次写文章管理文章不是特别方便,就使用laravel写了这个v ...
- 使用FastReport报表工具生成图片格式文档
之前我在随笔<使用FastReport报表工具生成报表PDF文档>介绍过使用FastReport.Net来根据报表模板进行生成PDF,以及随笔<使用FastReport报表工具生成标 ...