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. JSP页面无法使用EL导致"java.sql.SQLException: No suitable driver found for ${snapshot}"的问题

    使用JSTL来连接mysql,这个问题折腾了半天,老以为是Mysql驱动的问题,还好最后偶然发现了是EL表达式识别不了,报错: javax.servlet.ServletException: java ...

  2. vim—自动缩进(编写Python脚本)

    大神推荐使用vim编写Python脚本,学而时积之,不亦乐乎! 使用vim编写Python脚本的时候不能正常缩进,需要修改vimrc文件 Ubuntu系统下vimrc文件的位置: $ cd /etc/ ...

  3. 基于freeRTOS定时器实现闹钟(定时)任务

    基于freeRTOS定时器实现闹钟(定时)任务 在智能硬件产品中硬件中,闹钟定时任务是基本的需求.一般通过APP设置定时任务,从云端或者是APP直连硬件将闹钟任务保存在硬件flash中,硬件运行时会去 ...

  4. jquery取radio单选按钮

    // var strMess = '<%=Exchange() %>';//            if (strMess == "兑换成功") {//         ...

  5. 从oracle导入hive

    sqoop import --connect jdbc:oracle:thin:@10.39.1.43:1521/rcrm --username bi_query --password ####### ...

  6. python异步初步窥探

    1.异步之难:因为其执行吮吸不可预料,当下正要发生什么事件不可预料.        程序下一步行为往往依赖上一步值执行结果,如何知晓上次异步调用已完成并获取结果,        回调成了必然选择,那又 ...

  7. Java 动态绑定和多态

    动态绑定和多态 动态绑定是指:"在执行程序期间(而非编译期间),判断引用所指对象的实际类型,调用其相应的方法." 动态绑定(多态)存在的条件 要有继承. 要有重写. 父类引用指向子 ...

  8. a3

    队名 massivehard 组员一(组长:晓辉) 今天完成了哪些任务 .整理昨天的两个功能,补些bug 写了一个初步的loyaut 还剩哪些任务: 后台的用来处理自然语言的服务器还没架. 推荐算法还 ...

  9. JSP传递数组给JS的方法

    由于JSP页面的数组无法直接传到JS.所以采用以下方法来获取数组. <% String[] title = { "姓名 ", "学号 ", "性 ...

  10. Window命令行工具操作文件

    1,cd 命令用来切换目录 2,mkdir用来创建文件夹 3,rmdir用来删除空文件夹 4,创建指定类型的文件 type nul>"文件名和后缀" 5,打开指定文件用sta ...