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 ...
随机推荐
- js正则匹配的出链接地址
content为需要匹配的值 var b=/<a([\s]+|[\s]+[^<>]+[\s]+)href=(\"([^<>"\']*)\"| ...
- T-SQL Part VII: CROSS JOIN
虽然不能确定是不是只有个SQL Server提供了Cross Join的功能,貌似W3School的SQL教程中是没有的 SQL教程.而Wikipedia中倒是有,也是最新的SQL:2011SQL:2 ...
- C++中对封装的语法支持——重载运算符
重载运算符 1.对于自定义类型,编译器不知道运算规则,而重载运算符会将两个对象相加转换为函数调用. 2.运算符重载转换的函数调用,函数名字是固定的规则. (1) 如果重载+号运算符,函数名字就是:op ...
- 基于Windows下永久破解jetbrains公司的系列产品(Idea, pycharm,clion,phpstorm)
基于Windows下永久破解jetbrains公司的系列产品(Idea, pycharm,clion,phpstorm): PS : 有能力的建议购买正版,好吧. PS:均针对其对应的2018.3.1 ...
- opencv 5 图像转换(2 霍夫变换)
霍夫线变换 标准霍夫变换和多尺度霍夫变换(HoughLines()函数) 实例: #include <opencv2/opencv.hpp> #include <opencv2/im ...
- 📈📈📈📈📈iOS 图表框架 AAChartKit ---强大的高颜值数据可视化图表框架,支持柱状图、条形图、折线图、曲线图、折线填充图、曲线填充图、气泡图、扇形图、环形图、散点图、雷达图、混合图
English Document
- HTML、CSS基础知识
前端基础 1. CSS 8 1.1. CSS叫做层叠样式表,用来设置页面中元素的样式.背景颜色.字体颜色.字体大小... 8 1.2. CSS负责结构.表现.行为中的表现 8 1.3. 编写的位置 8 ...
- python--数字灯管
import turtle import time def drawLine(draw): #绘制单段数码管 turtle.pendown() if draw else turtle.penup() ...
- Redis高可用演进(一)
原文链接:http://www.cnblogs.com/chenty/p/5152878.html 最近整理Redis,对sentinel有了更深入的理解,特地总结如下 1.主从Redis 主从red ...
- 【Android - 进阶】之PopupWindow的使用
创建一个类继承自PopupWindow,编写自定义的PopupWindow类.示例代码如下: import android.app.Activity; import android.graphics. ...