THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8418    Accepted Submission(s): 2179

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
 
Source
 题意:
n*m的矩阵c,是否存在n个数a1,a2,...an,和m个数b1,b2,...bm使得L<=cij*ai/bj<=R.
代码:
//显然不满足差分约束的条件,可以L<=cij*ai/bj<=R两边除cij(cij>0)后取对数得到
//log(L/cij)<=log(ai)-log(bj)<=log(R/cij).只求存不存在就行。但是本体如果用stl
//的queue写spfa会超时(可以用节点出队次数小于sqrt(n)判断),可以自己定义一个栈来存储。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=;
const double inf=;
int n,m,tol,head[maxn*+],cnt[maxn*+],stk[maxn*maxn];
double L,R,dis[maxn*+];
bool mark[maxn*+];
struct node
{
int to,next;
double val;
}nodes[];
void Add(int a,int b,double c)
{
nodes[tol].to=b;
nodes[tol].val=c;
nodes[tol].next=head[a];
head[a]=tol++;
}
bool spfa(int s)
{
for(int i=;i<=n+m;i++){
dis[i]=inf;
cnt[i]=;
mark[i]=;
}
int top=;
stk[++top]=s;
mark[s]=;cnt[s]++;dis[s]=;
while(top>){
int u=stk[top--];
mark[u]=;
for(int i=head[u];i!=-;i=nodes[i].next){
int v=nodes[i].to;
if(dis[v]>dis[u]+nodes[i].val){
dis[v]=dis[u]+nodes[i].val;
if(!mark[v]){
mark[v]=;
if(++cnt[v]>=n+m) return ;
stk[++top]=v;
}
}
}
}
return ;
}
int main()
{
while(scanf("%d%d%lf%lf",&n,&m,&L,&R)==){
tol=;
memset(head,-,sizeof(head));
double x;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%lf",&x);
Add(j+n,i,log(R/x));
Add(i,j+n,-log(L/x));
}
}
for(int i=;i<=n+m;i++)//加一个公共源点0
Add(,i,);
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}

HDU3666 差分约束的更多相关文章

  1. 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 ...

  2. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  3. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  4. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  5. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  6. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

  7. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  8. [USACO2005][POJ3169]Layout(差分约束)

    题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...

  9. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

随机推荐

  1. 变量不加 var 声明——掉进坑中,无法自拔!

    整整一下午,都在解决 window.onresize 中方法丢失不执行的问题!姿势固定在电脑前,颈椎病都犯了. 前些日子与大家分享了一下关于 防止jquery $(window).resize()多次 ...

  2. ubuntu16.04图形界面安装中文输入法,中文展示

    打开system Settings 设置   打开设置语言   安装Language Support   点击installed languages 选择chinese 打勾,安装   安装IBus框 ...

  3. 【Paper】Deep & Cross Network for Ad Click Predictions

    目录 背景 相关工作 主要贡献 核心思想 Embedding和Stacking层 交叉网络(Cross Network) 深度网络(Deep Network) 组合层(Combination Laye ...

  4. Fluent Python: Classmethod vs Staticmethod

    Fluent Python一书9.4节比较了 Classmethod 和 Staticmethod 两个装饰器的区别: 给出的结论是一个非常有用(Classmethod), 一个不太有用(Static ...

  5. Thunder团队第二周 - Scrum会议7

    Scrum会议7 小组名称:Thunder 项目名称:i阅app Scrum Master:杨梓瑞 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...

  6. android入门 — AlertDialog对话框

    常见的对话框主要分为消息提示对话框.确认对话框.列表对话框.单选对话框.多选对话框和自定义对话框. 对话框可以阻碍当前的UI线程,常用于退出确认等方面. 在这里主要的步骤可以总结为: 1.创建Aler ...

  7. Nautilus-Share-Message: Called "net usershare info" but it failed: Failed to

    See what nautilus processes are running : ps aux | grep nautilus Kill all nautilus processes you see ...

  8. [CLR via C#]值类型的装箱和拆箱

    我们先来看一个示例代码: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Array ...

  9. Maven 3-Maven依赖版本冲突的分析及解决小结 (阿里,美团,京东面试)

    举例A依赖于B及C,而B又依赖于X.Y,而C依赖于X.M,则A除引B及C的依赖包下,还会引入X,Y,M的依赖包(一般情况下了,Maven可通过<scope>等若干种方式控制传递依赖).这里 ...

  10. File文件以及.propertites文件操作

    File文件操作 在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用Syste ...