THE MATRIX PROBLEM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7819    Accepted Submission(s): 2019

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
  1. 3 3 1 6
  2. 2 3 4
  3. 8 2 6
  4. 5 2 9
 
Sample Output
  1. YES
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3665 3669 3667 3664 3663 

刚开始用差分约束写的,我去,超时到最后!!!!后来找优化算法,slf跟lll都还没看,看到了深搜的SPFA

差分约束代码,别人提交的就能过,我的就超时,搞不懂,这一定不会是人品问题,对的,一定不会是!!!!!

  1. 超时代码:(应该是oj编译器问题或者就是AC的标准提高了)
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<queue>
  5. #include<cmath>
  6. #include<algorithm>
  7. using namespace std;
  8. #define MAXN 1000
  9. #define MAXM 500000+10
  10. #define INF 0x3f3f3f
  11. int dis[MAXN],vis[MAXN],used[MAXN],m,n;
  12. int head[MAXN],cnt;
  13. double map[MAXN][MAXN];
  14. double L,U;
  15. struct node
  16. {
  17. int u,v;
  18. double val;
  19. int next;
  20. }edge[MAXM];
  21. void init()
  22. {
  23. memset(head,-1,sizeof(head));
  24. memset(map,0,sizeof(map));
  25. cnt=0;
  26. }
  27. void add(int u,int v,int val)
  28. {
  29. node E={u,v,val,head[u]};
  30. edge[cnt]=E;
  31. head[u]=cnt++;
  32. }
  33. void getmap()
  34. {
  35. for(int i=1;i<=n;i++)
  36. {
  37. for(int j=1;j<=m;j++)
  38. {
  39. scanf("%lf",&map[i][j]);
  40. add(j+n,i,log(U/map[i][j]));
  41. add(i,j+n,-log(L/map[i][j]));
  42. }
  43. }
  44. for(int i=1;i<=n+m;i++)
  45. add(0,i,0);
  46. }
  47. void SPFA()
  48. {
  49. memset(vis,0,sizeof(vis));
  50. memset(used,0,sizeof(used));
  51. memset(dis,INF,sizeof(dis));
  52. queue<int>q;
  53. q.push(0);
  54. dis[0]=0;
  55. used[0]++;
  56. vis[0]=1;
  57. while(!q.empty())
  58. {
  59. int u=q.front();
  60. q.pop();
  61. vis[u]=0;
  62. for(int i=head[u];i!=-1;i=edge[i].next)
  63. {
  64. node E=edge[i];
  65. if(dis[E.v]>dis[E.u]+E.val)
  66. {
  67. dis[E.v]=dis[E.u]+E.val;
  68. if(!vis[E.v])
  69. {
  70. vis[E.v]=1;
  71. used[E.v]++;
  72. if(used[E.v]>(int)sqrt(1.0*n+m))
  73. {
  74. cout<<"NO"<<endl;
  75. return ;
  76. }
  77. q.push(E.v);
  78. }
  79. }
  80. }
  81. }
  82. cout<<"YES"<<endl;
  83. }
  84. int main()
  85. {
  86. while(scanf("%d%d%lf%lf",&n,&m,&L,&U)!=EOF)
  87. {
  88. init();
  89. getmap();
  90. SPFA();
  91. }
  92. return 0;
  93. }

SPFA深搜版


  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<math.h>
  6. #include<stack>
  7. #include<queue>
  8. using namespace std;
  9. const int MAX=805;
  10. struct node
  11. {
  12. int v,next;
  13. double c;
  14. }g[MAX*MAX];
  15. int adj[MAX];
  16. int n,m,e;
  17. double dis[MAX],l,u;
  18. bool vis[MAX],inStack[MAX];
  19. inline void add(int u,int v,double c)
  20. {
  21. g[e].v=v; g[e].c=c; g[e].next=adj[u]; adj[u]=e++;
  22. }
  23. bool spfa(int u)
  24. {
  25. int i,v;
  26. if(inStack[u])
  27. return false;
  28. inStack[u]=true;
  29. vis[u]=true;
  30. for(i=adj[u];i!=-1;i=g[i].next)
  31. {
  32. v=g[i].v;
  33. if(dis[v]>dis[u]+g[i].c)
  34. {
  35. dis[v]=dis[u]+g[i].c;
  36. if(!spfa(v))
  37. {
  38. return false;
  39. }
  40. }
  41. }
  42. inStack[u]=false;
  43. return true;
  44. }
  45. bool ok()
  46. {
  47. int i,u,v,cnt=0;
  48. memset(vis,0,sizeof(vis));
  49. memset(inStack,0,sizeof(inStack));
  50. for(i=0;i<=n+m;i++)
  51. {
  52. dis[i]=0;
  53. }
  54. for(i=1;i<=n+m;i++)
  55. {
  56. if(!vis[i])
  57. {
  58. if(!spfa(i))
  59. {
  60. return false;
  61. }
  62. }
  63. }
  64. return true;
  65. }
  66. int main()
  67. {
  68. int i,j;
  69. double t;
  70. while(scanf("%d%d %lf %lf",&n,&m,&l,&u)!=EOF)
  71. {
  72. e=0;
  73. memset(adj,-1,sizeof(adj));
  74. for(i=1;i<=n;i++)
  75. {
  76. for(j=1;j<=m;j++)
  77. {
  78. scanf("%lf",&t);
  79. add(j+n,i,log(u/t));
  80. add(i,j+n,-log(l/t));
  81. }
  82. }
  83. if(ok())
  84. puts("YES");
  85. else
  86. puts("NO");
  87. }
  88. return 0;
  89. }

hdoj--3666--THE MATRIX PROBLEM(差分约束+SPFA深搜)的更多相关文章

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

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

  5. hduTHE MATRIX PROBLEM(差分约束)

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

  6. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

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

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

  8. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  9. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

随机推荐

  1. 2016.03.02,英语,《Vocabulary Builder》Unit 03

    ambi/amphi: 指on both sides或者around的意思,ambi-来自拉丁语,amphi-来自希腊语.ambidextrous:[ˌæmbi'dekstrəs] adj. 两手俱利 ...

  2. jquery中命名冲突问题

    例如用jq代替$符号 var jq = $.noConflict()

  3. C# 遍历文件夹及子目录下所有图片.

    要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径. 服务端代码: public partial class ViewIcon : System.Web.UI.Page { JAr ...

  4. oracle数据泵备份与还原

    完整的常用的一套oracle备份以及还原方案 --在新库中新建数据目录,我没有特别说明在哪执行的语句都可在plsql中执行 CREATE OR REPLACE DIRECTORY dump_dir A ...

  5. SQL Server的三种分页方式

    直接上代码 --top not in方式 select top 条数 * from tablename where Id not in (select top 条数*页数 Id from tablen ...

  6. Python笔记(七)

    # -*-coding:utf-8-*- # Python 文件I/O # 打印到屏幕 #print 1234567 # 读取屏幕输入 #input_str=raw_input("Pleas ...

  7. R dataframe 去除行号

    原先的行号是这样的:

  8. golang vue nginx

    https://segmentfault.com/a/1190000012780963 https://blog.csdn.net/qq_32340877/article/details/790321 ...

  9. .csv文件内容分隔符

    CSV文件默认以英文逗号做为列分隔符,换行符作为行分隔符.  如果不提供网页形式只用命令行或二进制程序输出数据到CSV,只需要将数据段按,分割,行按\n分割,写入到一个.csv文件即可.  但有时字段 ...

  10. asp实现阿里大鱼短信API接口的方法

    阿里大鱼是阿里推出的产品,官方提供JAVA..NET.PHP等版本的SDK下载,不知为何,唯独不提供ASP版本的SDK. 不提供没关系,自己写就是了,参照官方提供的API写一个就是了. 本来以为无非是 ...