THE MATRIX PROBLEM

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

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
 
题意:是否存在数组a,b使得l/G[i][j]<=a[i]/b[j]<=u/G[i][j]
思路:乘除变加减取log,加减边乘除去指数。问题变成log2(l)-log2(G[i][j])<=log2(a[i])-log2(b[j])<=log2(u)-log(G[i][j]),这是差分约束系统是否有解,即最短路求解,是否存在负圈。spfa算法,当所有入队列的次数>2*n,即存在负圈,或者每个点入队列的次数>n,其实>sqrt(n+1)就可以了。
dist[i]表示起点s到i的最短距离,对于<u,v>,则dist[u]+w>=dist[v]。所以dist[v]-diat[u]<=w; 
代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<set>
  7. #include<bitset>
  8. #include<map>
  9. #include<queue>
  10. #include<stack>
  11. #include<vector>
  12. using namespace std;
  13. #define bug(x) cout<<"bug"<<x<<endl;
  14. #define PI acos(-1.0)
  15. #define eps 1e-8
  16. typedef long long ll;
  17. typedef pair<int,int> P;
  18. const int N=,M=1e6;
  19. const int inf=0x3f3f3f3f;
  20. const ll mod=1e9+;
  21. const double INF=;
  22. struct edge
  23. {
  24. int from,to;
  25. double w;
  26. int next;
  27. };
  28. int n,m;
  29. edge es[M];
  30. int cut,head[N];
  31. double dist[N];
  32. void init()
  33. {
  34. cut=;
  35. memset(head,-,sizeof(head));
  36. }
  37. void addedge(int u,int v,double w)
  38. {
  39. ///cout<<u<<" ** "<<v<<" ** "<<w<<endl;
  40. cut++;
  41. es[cut].from=u,es[cut].to=v;
  42. es[cut].w=w;
  43. es[cut].next=head[u];
  44. head[u]=cut;
  45. }
  46. bool spfa()
  47. {
  48. int cou=;
  49. queue<int>q;
  50. bool inq[N];
  51. for(int i=; i<=n+m+; i++) dist[i]=inf,inq[i]=false;
  52. dist[]=;
  53. q.push();
  54. while(!q.empty())
  55. {
  56. int u=q.front();
  57. q.pop();
  58. inq[u]=false;
  59. if(++cou>*(n+m)) return false;
  60. for(int i=head[u]; i!=-; i=es[i].next)
  61. {
  62. edge e=es[i];
  63. if(dist[e.to]>dist[e.from]+e.w)
  64. {
  65. dist[e.to]=dist[e.from]+e.w;
  66. ///cout<<e.from<<" * "<<e.to<<" * "<<dist[e.to]<<endl;
  67. if(!inq[e.to]) q.push(e.to),inq[e.to]=true;
  68. }
  69. }
  70. }
  71. return true;
  72. }
  73. int main()
  74. {
  75. double l,u;
  76. while(scanf("%d%d%lf%lf",&n,&m,&l,&u)!=EOF)
  77. {
  78. init();
  79. for(int i=; i<=n; i++)
  80. {
  81. for(int j=; j<=m; j++)
  82. {
  83. double g;
  84. scanf("%lf",&g);
  85. addedge(i,n+j,log2(g)-log2(l));
  86. addedge(n+j,i,log2(u)-log2(g));
  87. }
  88. }
  89. if(spfa()) puts("YES");
  90. else puts("NO");
  91. }
  92. return ;
  93. }

差分约束

HDU 3666.THE MATRIX PROBLEM 差分约束系统的更多相关文章

  1. HDU 3666 THE MATRIX PROBLEM (差分约束)

    题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...

  2. HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

    题意: 给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存 ...

  4. hdu 3666 THE MATRIX PROBLEM

    差分约束系统. 根据题意,可以写出不等式 L <= (Xij * Ai) / Bj <= U 即 Ai/Bj<=U/Xij和Ai/Bj>=L/Xij 由于差分约束系统是减法.. ...

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

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

  7. hduTHE MATRIX PROBLEM(差分约束)

    题目请戳这里 题目大意:给一个n*m的矩阵,求是否存在这样两个序列:a1,a2...an,b1,b2,...,bm,使得矩阵的第i行乘以ai,第j列除以bj后,矩阵的每一个数都在L和U之间. 题目分析 ...

  8. ZOJ 1455 Schedule Problem(差分约束系统)

    // 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...

  9. 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. Excel--按内容分页打印

    当我们有这样一张表,需要按不同城市分页打印,每页带标题行,可按以下步骤:1.点击城市一列任一单元格,点击“开始”——>“排序和筛选”(升序): 2.点击“数据”-->“分类汇总”: 分类字 ...

  2. 关于Spring的那点事

    一.Spring约束 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="htt ...

  3. mmap映射区和shm共享内存的区别总结

    [转载]原文链接:https://blog.csdn.net/hj605635529/article/details/73163513 linux中的两种共享内存.一种是我们的IPC通信System ...

  4. 导出Excel实现 (ASP.NET C# 代码部分)

    背景: 实现导出Excel功能. 技术: ASP.NET  , 采用`Aspose.Cells`第三方组件, C# 实现通用部分. 根据前台Ext Grid完成导入Excel中文列与实际存储列的对应关 ...

  5. NFS服务配置

    FS服务会经常用于在网络上共享存储. 比如有3台机子A,B,C;他们都需要访问同一个目录,使用NFS, 只需要把图片都放在A上,然后A共享给B和C即可. 访问B和C时,是通过网络的方式访问A上的哪个目 ...

  6. Java笔试面试题整理第三波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

  7. JAVA中循环删除list中元素的方法总结(同上篇)

    印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...

  8. python使用xlrd读取excel数据时,整数变小数的解决办法

    python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...

  9. Idea中运行项目时出现:未结束的字符串解决方案

    一般出现这种情况是编码不一致导致 解决办法: settings>file Encodings 编码设置成一致

  10. SpringCloud系列二:Restful 基础架构(搭建项目环境、创建 Dept 微服务、客户端调用微服务)

    1.概念:Restful 基础架构 2.具体内容 对于 Rest 基础架构实现处理是 SpringCloud 核心所在,其基本操作形式在 SpringBoot 之中已经有了明确的讲解,那么本次为 了清 ...