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 ...
随机推荐
- java多线程与线程并发二:线程互斥
本文章内容整理自:张孝祥_Java多线程与并发库高级应用视频教程 当两条线程访问同一个资源时,可能会出现安全隐患.以打印字符串为例,先看下面的代码: // public class Test2 { p ...
- sso单点登录系统
sso单点登录概念 1.一处登录,处处登录.会单独做一个单点登录系统,只负责颁发token和验证token,和页面登录功能. 2.通过在浏览器cookie中放入token,和在redis中对应toke ...
- 微服务SpringCloud之GateWay熔断、限流、重试
纯洁的微笑的Spring Cloud系列博客终于学完了,也对Spring Cloud有了初步的了解. 修改请求路径的过滤器 StripPrefix Filter 是一个请求路径截取的功能,我们可以利用 ...
- 用正则表达式获取URL中的查询参数
总结获取url中查询参数的两种方式 通过正则表达式获取单个参数 url中的所有查询参数可以通过 window.location.search 字段获取,以字符串的形式返回.并有固定的格式 ?param ...
- SqlServer2005 查询 第八讲 order by
今天我们来说模糊查询 模糊查询 -- --模糊查询[主要用在搜索中]格式:select 字段的集合 from 表名 where 某个字段名 like 匹配条件 --注意:匹配条件通常含有通配符,通配符 ...
- Docker解决下载镜像速度慢
Docker安装好以后要用Docker pull命令下载镜像,但是会出现下载很慢的现象.Docker默认是国外的源,配置国内镜像仓库. 1.cd /etc/docker/路径下 2.编辑daemon. ...
- shell脚本3——调试
bash -x file.sh 这样会把执行到的语句全部打印出来 #!/bin/bash 不会打印的程序块 set -v 需要打印的程序块 set -v 不会打印的程序块
- InfluxDB 聚合函数实用案例
InfluxDB 聚合函数实用案例 文章大纲 InfluxDB 简介 InfluxDB是GO语言编写的分布式时间序列化数据库,非常适合对数据(跟随时间变化而变化的数据)的跟踪.监控和分析.在我们的项目 ...
- vim常用插件使用方法整理【持续更】
nerdtree 和编辑文件一样,通过h j k l移动光标定位切换工作台和目录 ctr+w+h 光标focus左侧树形目录,ctrl+w+l 光标focus右侧文件显示窗口. ctrl+w+w,光标 ...
- GeoServer 修改端口
准备内容 安装环境:win10*64位专业版 安装文件:geoserver-2.15.2 操作步骤 1.找到文件夹下的start.ini,并用记事本打开 2.找到jetty.port,修改为自己需要的 ...